objective-c – iOS 8在UITextView中为彩色字符串的表情符号图标后自动添加空格

我在我的应用程序中使用Apple TextKit IntroToTextKitDemo2013示例代码.

以下是示例代码link

如果它是’Alice’或’Rabbit’,它会动态改变UITextView AttributedString的颜色

当我输入“#”符号时,它可以正常显示,但是当我在表情符号图标后输入“#”时,它会在表情符号和“#”符号之后添加空格.
我使用的是iOS 8.3

这是我的代码

    //ControllerCode

@property (nonatomic, retain) TKDInteractiveTextColoringTextStorage *textStorage;

-(void)viewDidLoad
{

    [super viewDidLoad];

    // our auto layout views use a design spec that calls for
    // 8 pts on each side except the bottom
    // since we scroll at the top here, only inset the sides

    CGRect newTextViewRect = CGRectInset(self.view.bounds, 8., 0.);

    self.textStorage = [[TKDInteractiveTextColoringTextStorage alloc] init];

    NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];

    NSTextContainer *container = [[NSTextContainer alloc] initWithSize:CGSizeMake(newTextViewRect.size.width, CGFLOAT_MAX)];
    container.widthTracksTextView = YES;
    [layoutManager addTextContainer:container];
    [_textStorage addLayoutManager:layoutManager];

    UITextView *newTextView = [[UITextView alloc] initWithFrame:newTextViewRect textContainer:container];
    newTextView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    newTextView.scrollEnabled = YES;
    newTextView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;

    [self.view addSubview:newTextView];
    self.textView = newTextView;

    self.textStorage.tokens = @{ @"Alice" : @{ NSForegroundColorAttributeName : [UIColor redColor] },
                                 @"Rabbit" : @{ NSForegroundColorAttributeName : [UIColor orangeColor] },
                                 TKDDefaultTokenName : @{ NSForegroundColorAttributeName : [UIColor blackColor] } };
}

-(void)setDemo:(TKDDemo *)demo
{

    [super setDemo:demo];
    (void)[self view];

    [_textStorage beginEditing];
    [_textStorage setAttributedString:self.demo.attributedText];
    [_textStorage endEditing];
}

最佳答案 有类似的问题,将Font添加到属性字符串并且对我来说工作正常

[attributedString addAttribute:NSFontAttributeName
                         value:[UIFont fontWithName:@"ProximaNova-Regular" size:16.0]
                         range:NSMakeRange(0, [self.capTextView.text length])];
点赞