如何设置UILabel lineBreakMode来破坏单词并为断字添加连字符?
一个破碎的标签 -
rd应该是这样的
我希望在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]; …Run Code Online (Sandbox Code Playgroud) 我正试图在左边有一个带有图像和标题的UIlabel,在右边有一个带有项目符号的描述列表,为此我正在使用NSAttributedString,如下所示:
NSMutableParagraphStyle *pStyle = [[NSMutableParagraphStyle alloc] init];
pStyle.tabStops =
@[ [[NSTextTab alloc] initWithTextAlignment:NSTextAlignmentLeft location:tabLocation options:[NSDictionary dictionary]] ];
NSMutableAttributedString *attString = [[NSMutableAttributedString alloc] init];
NSTextAttachment *textAttachment = [[NSTextAttachment alloc] init];
textAttachment.image = [UIImage imageNamed:@"test_image"];
textAttachment.bounds = CGRectMake(0, -3, 15, 15);//resize the image
attString = [NSAttributedString attributedStringWithAttachment:textAttachment].mutableCopy;
[attString appendAttributedString:[[NSAttributedString alloc]
initWithString:[NSString stringWithFormat:@"title\t\u2022 %@",
[@[ @"description1", @"description2" ]
componentsJoinedByString:@"\n\t\u2022 "]]
attributes:@{NSParagraphStyleAttributeName : pStyle}]];
label.attributedText = attString;
Run Code Online (Sandbox Code Playgroud)
我希望右边的列表保持对齐但事实并非如此,这是我得到的结果:
有没有办法限制NSAttributedString段落中的行数?
我在NSAttributedString中追加两个字符串,我希望它们最多3行,第一个字符串将是1-2行,如果需要则截断.第二个字符串应该总是在最后一行上
:
this is my first string
if its too long i't will get trun...
But this is my second string
Run Code Online (Sandbox Code Playgroud)
我做的是:
// First string
NSAttributedString *first = [[NSAttributedString alloc] initWithString:@"this is my first string if its too long i't will get trunticated"
attributes:@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont fontWithName:@"HelveticaNeue-Light" size:17.0]];
[str appendAttributedString:first];
// New line
[str appendAttributedString:[[NSAttributedString alloc] initWithString:@"\n"]];
// Add photo count
NSAttributedString *second = [[NSAttributedString alloc] initWithString:@"But this is my second string"
attributes:@{NSForegroundColorAttributeName:[UIColor redColor],
NSFontAttributeName:[UIFont …Run Code Online (Sandbox Code Playgroud) 我有一个包含UITextView的子类UITableViewCell.我已将NSParagraphStyle作为属性添加到子类NSTextStorage中的字符串中.在下面的代码中,我增加了UITextView中每行之间的空间.
迅速
let paragraphStyle = NSMutableParagraphStyle()
paragraphStyle.lineSpacing = 11
myCustomTextStorage.appendAttributedString(NSAttributedString(string: someText, attributes: [NSParagraphStyleAttributeName: paragraphStyle]))
Run Code Online (Sandbox Code Playgroud)
光标高度延长到下一行的高度,如下所示.这仅发生在最后一行之前的行上.

关于这个问题,我已经看了几篇关于SO的帖子,包括这篇文章 ; 但是,提议的解决方案似乎都不适合我.
我已经阅读了TextKit文档,但没有找到任何解决此问题的方法.
有没有办法降低光标高度?
我已经创建了一个普通的UILabel,并且希望能够为在UILabel中添加文本添加行距。
虽然在执行此操作时会影响adjustsFontSizeToFitWidth,并且不再适合UILabel。
我使用了一些代码:
var userQuestionsAnswer = UILabel(frame: CGRectMake(0, 0, 20, 20))
userQuestionsAnswer.font = UIFont(name: Font.GothamBlack, size: 15)
userQuestionsAnswer.numberOfLines = 0
userQuestionsAnswer.adjustsFontSizeToFitWidth = true
var style = NSMutableParagraphStyle()
style.lineSpacing = view.frame.size.height * 0.021
style.alignment = NSTextAlignment.Center
let attributes = [NSParagraphStyleAttributeName : style]
var answerText = "This is the answer"
self.userQuestionsAnswer.attributedText = NSAttributedString(string: answerText!, attributes:attributes)
Run Code Online (Sandbox Code Playgroud)
谁能告诉我这为什么以及我如何解决?
我有一个NSAttributedString"表"使用NSTextTab停止,其中第一列是KEY,第二列是值
像这样:
| **KEY 1**|value1|
|**KEY TWO**|value2|
Run Code Online (Sandbox Code Playgroud)
KEYS的字体大小/重量不同+全部大写.行对齐,以便所有字母的底部匹配.我想垂直微调这些值,使它们看起来与KEYS垂直居中.
有没有办法做到这一点?这似乎非常专业,但从设计的角度来看,我认为它看起来会更好
objective-c nsattributedstring core-text ios nsparagraphstyle
我正在尝试为NSTextView指定行数.我的设计师要求最多两行文字.我已经尝试过NSMutableParagraph样式来添加我想要的省略号截断,但是使用NSMutableParagraph我只能得到带有1行的NSTextView而没有NSMutableParagraph,我得到一个滚动文本,其中包含完成文本所需的行数.
var attributedString = NSMutableAttributedString(string: "This is my text, I can keep going for many characters")
var para = NSMutableParagraphStyle()
para.lineBreakMode = NSLineBreakMode.ByTruncatingTail
let globalAttributes = [
NSParagraphStyleAttributeName: para
]
let range = NSRange(location:0, length: attributedString.length)
attributedString.addAttributes(globalAttributes, range: range)
cellView.myTextView!.textStorage?.setAttributedString(attributedString)
Run Code Online (Sandbox Code Playgroud)
我在NSTextView上尝试过高度限制.我试过了:
cellView.myTextView!.textContainer?.containerSize = NSMakeSize(300, 32)
Run Code Online (Sandbox Code Playgroud)
我已经尝试为NSScrollView创建IBOutlet NSTextView并调整其高度.获得两条线和截断都没有运气.任何帮助是极大的赞赏.我觉得我只是错过了方法或设置.谢谢!
macos cocoa nstextview nsmutableattributedstring nsparagraphstyle
我正在尝试实现这样的事情(灰色文本始终位于textview的底部):
但是当我将换行模式设置为NSLineBreakByTruncatingTail它时,每个段落只能允许一行文本,如下所示:
我想将第一段截断为最多2行,并且将灰色文本始终作为文本的最后一行。
我正在尝试为WKInterfaceLabel使用setAttributedText功能设置对齐方式。这是我的代码:
var paragraphStyle = NSParagraphStyle.defaultParagraphStyle()
paragraphStyle.alignment = NSTextAlignment.Center
var attributedDictonary = [NSForegroundColorAttributeName:UIColor.greenColor(), NSParagraphStyleAttributeName:paragraphStyle]
var attributeString = NSAttributedString(string: "TextAttributed", attributes: attributedDictonary)
self.titleLabel.setAttributedText(attributeString)
Run Code Online (Sandbox Code Playgroud)
但是我在这条线上遇到了问题:
paragraphStyle.alignment = NSTextAlignment.Center
Run Code Online (Sandbox Code Playgroud)
我有错误: Cannot assign to 'alignment' in 'paragraphStyle'
如何设置对齐WKInterfaceLabel使用setAttributedText?
setattribute swift nsparagraphstyle xcode-6.2 wkinterfacelabel
我想增加 UILabel 中的行距,但我不知道如何增加。
我在 Stackoverflow 上找到了这个解决方案 [ /sf/answers/2741108891/],但我的 Xcode 总是显示以下内容:
Use of unresolved identifier 'NSParagraphStyleAttributeName'
Run Code Online (Sandbox Code Playgroud)
我认为答案是正确的,但它对我不起作用。任何人都可以帮助解决这个问题吗?
nsparagraphstyle ×12
ios ×8
swift ×5
uilabel ×4
core-text ×2
line-spacing ×2
objective-c ×2
uitextview ×2
cocoa ×1
hyphen ×1
macos ×1
nsstring ×1
nstexttab ×1
nstextview ×1
setattribute ×1
textkit ×1
word-wrap ×1
xcode ×1
xcode-6.2 ×1