gpt4 book ai didi

ios - 多行文本的选定 UITextRange 调整的 CGRect?

转载 作者:技术小花猫 更新时间:2023-10-29 10:26:55 25 4
gpt4 key购买 nike

我用过 this answer以便为特定范围的文本创建 CGRect。

在这个 UITextView 中,我将其设置为 attributedText(所以我得到了一堆具有不同字形大小的样式文本)。

这对于左对齐的第一行文本非常有效,但在使用 NSTextAlignmentJustifiedNSTextAlignmentCenter 时会产生一些非常奇怪的结果。

当换行或(有时)有 \n 换行符时,它也无法正确计算。

我得到这样的东西(这是居中对齐的):

enter image description here

当我期望这样的时候:

enter image description here

这个有一个\n换行符——前两个代码位被高亮成功,但是最后一个more code for you see不是因为文本包装不计入 x,y 计算。

这是我的实现:

- (void)formatMarkdownCodeBlockWithAttributes:(NSDictionary *)attributesDict
withHighlightProperties:(NSDictionary *)highlightProperties
forFontSize:(CGFloat)pointSize
{
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"`.+?`" options:NO error:nil];
NSArray *matchesArray = [regex matchesInString:[self.attributedString string] options:NO range:NSMakeRange(0, self.attributedString.length)];
for (NSTextCheckingResult *match in matchesArray)
{
NSRange range = [match range];
if (range.location != NSNotFound) {

self.textView.attributedText = self.attributedString;

CGRect codeRect = [self frameOfTextRange:range forString:[[self.attributedString string] substringWithRange:range] forFontSize:pointSize];
UIView *highlightView = [[UIView alloc] initWithFrame:codeRect];
highlightView.layer.cornerRadius = 4;
highlightView.layer.borderWidth = 1;
highlightView.backgroundColor = [highlightProperties valueForKey:@"backgroundColor"];
highlightView.layer.borderColor = [[highlightProperties valueForKey:@"borderColor"] CGColor];
[self.contentView insertSubview:highlightView atIndex:0];

[self.attributedString addAttributes:attributesDict range:range];

//strip first and last `
[[self.attributedString mutableString] replaceOccurrencesOfString:@"(^`|`$)" withString:@" " options:NSRegularExpressionSearch range:range];
}
}
}

- (CGRect)frameOfTextRange:(NSRange)range forString:(NSString *)string forFontSize:(CGFloat)pointSize
{
self.textView.selectedRange = range;
UITextRange *textRange = [self.textView selectedTextRange];
CGRect rect = [self.textView firstRectForRange:textRange];
//These three lines are a workaround for getting the correct width of the string since I'm always using the monospaced Menlo font.
rect.size.width = ((pointSize / 1.65) * string.length) - 4;
rect.origin.x+=2;
rect.origin.y+=2;
return rect;
}

哦,如果你想要的话,这是我正在玩的字符串:

*This* is **awesome** @mention `code` more \n `code and code` #hashtag [markdown](http://google.com) __and__ @mention2 {#FFFFFF|colored text} This**will also** work but ** will not ** **work** Also, some `more code for you to see`

注意:请不要建议我使用TTTAtributedLabelOHAttributedLabel

最佳答案

我认为你所有的问题都是因为指令顺序不正确。

你必须

  1. 设置文本对齐方式
  2. 找到所需的子字符串并向其添加特定属性
  3. 然后才用 subview 高亮显示字符串。

在这种情况下,您也不需要使用“获得正确字符串宽度的解决方法,因为我总是使用等宽 Menlo 字体”。

我稍微简化了您的代码以使其更易于理解。

结果: enter image description here

- (void)viewDidLoad
{
[super viewDidLoad];

NSDictionary *basicAttributes = @{ NSFontAttributeName : [UIFont boldSystemFontOfSize:18],
NSForegroundColorAttributeName : [UIColor blackColor] };
NSDictionary *attributes = @{ NSFontAttributeName : [UIFont systemFontOfSize:15],
NSForegroundColorAttributeName : [UIColor darkGrayColor]};


_textView.attributedText = [[NSAttributedString alloc] initWithString:
@"*This* is **awesome** @mention `code` more \n `code and code` #hashtag [markdown](http://google.com) __and__ @mention2 {#FFFFFF|colored text} This**will also** work but ** will not ** **work** Also, some `more code for you to see`" attributes:attributes];
_textView.textAlignment = NSTextAlignmentCenter;

[self formatMarkdownCodeBlockWithAttributes:basicAttributes];
}

- (void)formatMarkdownCodeBlockWithAttributes:(NSDictionary *)attributesDict
{
NSMutableString *theString = [_textView.attributedText.string mutableCopy];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"`.+?`" options:NO error:nil];
NSArray *matchesArray = [regex matchesInString:theString options:NO range:NSMakeRange(0, theString.length)];

NSMutableAttributedString *theAttributedString = [_textView.attributedText mutableCopy];
for (NSTextCheckingResult *match in matchesArray)
{
NSRange range = [match range];
if (range.location != NSNotFound) {
[theAttributedString addAttributes:attributesDict range:range];
}
}

_textView.attributedText = theAttributedString;

for (NSTextCheckingResult *match in matchesArray)
{
NSRange range = [match range];
if (range.location != NSNotFound) {

CGRect codeRect = [self frameOfTextRange:range];
UIView *highlightView = [[UIView alloc] initWithFrame:codeRect];
highlightView.layer.cornerRadius = 4;
highlightView.layer.borderWidth = 1;
highlightView.backgroundColor = [UIColor yellowColor];
highlightView.layer.borderColor = [[UIColor redColor] CGColor];
[_textView insertSubview:highlightView atIndex:0];
}
}
}

- (CGRect)frameOfTextRange:(NSRange)range
{
self.textView.selectedRange = range;
UITextRange *textRange = [self.textView selectedTextRange];
CGRect rect = [self.textView firstRectForRange:textRange];
return rect;
}

关于ios - 多行文本的选定 UITextRange 调整的 CGRect?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22362131/

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