gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 19:45:39 25 4
gpt4 key购买 nike

我在我的应用程序中使用 apple TextKit IntroToTextKitDemo2013 示例代码。

这是示例代码 link

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

当我输入“#”符号时,它工作正常,如图所示,但当我在表情符号图标后输入“#”时,它会在表情符号和“#”符号后添加空格。我正在使用 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];
}

Space added after #

# without space

最佳答案

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

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

关于objective-c - iOS 8 在 UITextView 中为彩色字符串的表情符号图标后自动添加空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29841571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com