gpt4 book ai didi

ios - 在 UITextView 中画一条线 - NSAttributedString

转载 作者:可可西里 更新时间:2023-11-01 03:18:26 24 4
gpt4 key购买 nike

我希望在包含一些文本的 UITextView 中绘制一条可自定义的线(使用 NSAttributedString)

这是我尝试过的

NSString *unicodeStr = [NSString stringWithFormat:@"%C%C%C", 0x00A0, 0x0009, 0x00A0]; //nbsp, tab, nbsp
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:unicodeStr];
NSRange strRange = NSMakeRange(0, str.length);

NSMutableParagraphStyle *const tabStyle = [[NSMutableParagraphStyle alloc] init];
tabStyle.headIndent = 16; //padding on left and right edges
tabStyle.firstLineHeadIndent = 16;
tabStyle.tailIndent = -16;
NSTextTab *listTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentCenter location:40 options:@{}]; //this is how long I want the line to be
tabStyle.tabStops = @[listTab];
[str addAttribute:NSParagraphStyleAttributeName value:tabStyle range:strRange];
[str addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:strRange];

但无论我为制表位位置(在本例中为 40)和 tailIndent(此处为 -16)提供什么值,该行仅考虑 headIndent 并跨越整个 UITextView 宽度(当然减去 headIndent)。

编辑 - 我很确定这个问题是因为我没有使用正确的 unicode 字符(尽管它们似乎是合乎逻辑的选择)。万一这给了某人提示,如果我在第二个 nbsp 之后添加一个空格,即接近尾声,则选项卡仅限于单个选项卡长度

最佳答案

这是您期望的结果吗?

enter image description here enter image description here

你能试试这个吗:

NSTextTab *listTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentCenter location:self.textView.frame.size.width - tabStyle.firstLineHeadIndent + tabStyle.tailIndent options:@{}];

这是完整的代码:

- (void) viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
NSString *unicodeStr = @"\n\u00a0\t\t\n";
NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:unicodeStr];
NSRange strRange = NSMakeRange(0, str.length);

NSMutableParagraphStyle *const tabStyle = [[NSMutableParagraphStyle alloc] init];
tabStyle.headIndent = 16; //padding on left and right edges
tabStyle.firstLineHeadIndent = 16;
tabStyle.tailIndent = -70;
NSTextTab *listTab = [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentCenter location:self.textView.frame.size.width - tabStyle.headIndent + tabStyle.tailIndent options:@{}]; //this is how long I want the line to be
tabStyle.tabStops = @[listTab];
[str addAttribute:NSParagraphStyleAttributeName value:tabStyle range:strRange];
[str addAttribute:NSStrikethroughStyleAttributeName value:[NSNumber numberWithInt:2] range:strRange];

NSAttributedString *htmlStr = [[NSAttributedString alloc] initWithData:[@"<h1>Lorem ipsum dolor sit er elit lamet</h1>" dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];

[str insertAttributedString:htmlStr atIndex:0];
[str appendAttributedString:htmlStr];

self.textView.attributedText = str;
}

关于ios - 在 UITextView 中画一条线 - NSAttributedString,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35942355/

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