[英]Draw a line inside a UITextView - NSAttributedString
我希望在包含一些文本的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 之后添加一個空格,即在最后,選項卡僅限於單個選項卡長度
是你預期的結果嗎?
你能試試這個嗎:
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;
}
這是我為解決同一問題所做的工作。 它使用NSTextTab
的子類:
import UIKit
class RightAnchoredTextTab : NSTextTab {
weak var textContainer : NSTextContainer!
required init(textAlignment alignment: NSTextAlignment, location loc: CGFloat, options: [String : AnyObject], textContainer aTextContainer : NSTextContainer) {
super.init(textAlignment: alignment, location: loc, options: options)
textContainer = aTextContainer
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override var location: CGFloat {
get {
return textContainer.size.width-textContainer.lineFragmentPadding*2-super.location
}
}
}
與其他解決方案類似的類似位:
func horizontalLine(indent : CGFloat = 30, width : CGFloat = 1) -> NSAttributedString
{
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.tabStops = []
paragraphStyle.addTabStop(NSTextTab(textAlignment: .Left, location: indent, options: [:]))
paragraphStyle.addTabStop(RightAnchoredTextTab(textAlignment: .Right, location: indent, options: [:], textContainer : textView.textContainer))
paragraphStyle.alignment = .Left
let attributes = [NSParagraphStyleAttributeName : paragraphStyle, NSStrikethroughStyleAttributeName : width]
textView.backgroundColor = UIColor.yellowColor()
let ZeroWidthNonBreakingSpace = "\u{FEFF}"
return NSAttributedString(string: "\t\(ZeroWidthNonBreakingSpace)\t\(ZeroWidthNonBreakingSpace)\n", attributes: attributes)
}
一些注意事項:
NSTextContainer
有一個lineFragmentPadding
。 如果您想知道為什么必須將右對齊制表符縮進 10,那么就是這樣:默認值為 5。\\n
前綴文本意味着沒有刪除線。 不知道為什么。UITextView
在更改邊界時再次調用NSTextTab.location
。快速 5,
let unicodeStr = "\n\u{00a0}\t\t\n"
let str = NSMutableAttributedString(string: unicodeStr)
let strRange = NSRange(location: 0, length: str.length)
let tabStyle = NSMutableParagraphStyle()
tabStyle.headIndent = 0 //padding on left and right edges
tabStyle.firstLineHeadIndent = 0
//tabStyle.tailIndent = -16
let listTab = NSTextTab(textAlignment: .center, location: textView.frame.size.width-10, options: [:]) //this is how long I want the line to be
tabStyle.tabStops = [listTab]
str.addAttribute(.paragraphStyle, value: tabStyle, range: strRange)
str.addAttribute(.strikethroughStyle, value: NSNumber(value: 2), range: strRange)
str.addAttribute(.strikethroughColor, value: UIColor.label, range: strRange)
str.addAttribute(NSAttributedString.Key.font, value: self.currentFont, range: strRange)
str.addAttribute(NSAttributedString.Key.foregroundColor, value: UIColor.label, range: strRange)
textView.textStorage.append(str)
//To set cursor end of document
let endPosition = self.textView.endOfDocument
self.textView.selectedTextRange = self.textView.textRange(from: endPosition, to: endPosition)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.