我正在给UILabel边境
Label.text = lbltext;
Label.layer.borderColor = [[UIColor grayColor] CGColor];
Label.layer.borderWidth = 2;
Run Code Online (Sandbox Code Playgroud)
但是文本和边框之间没有空格.
那么如何在我的标签中设置像UIButton这样的插入效果呢?
我正在使用AutoLayout将一些标签放在单元格的垂直中心.文本是全部大写的,但UILabel问题是,即使sizeToFit应用,也会在文本下方留下空间,这看起来很像字母上的尾部,例如小写的y,p和q.因为我是垂直居中的,所以这会导致偏移并且意味着文本看起来比应该做的高几个像素.
另一个问题可能是:我可以根据字体是否包含使用上升器或下降器的任何字符来智能地调整其垂直中心吗?
例如,字符串"abbaba"不需要下降器,而字符串"oyyoyo"不需要上升器.全部大写字符串也不需要下降器.如果我垂直居中"oyyoyoyo"它会显得太低.

如果它是我可以做的TextView
self.textView.textContainer.lineFragmentPadding = 0;
Run Code Online (Sandbox Code Playgroud)
但我所拥有的是多线标签.如何删除填充?
我的快速应用程序中有一些带有边框的标签。我在标签上添加了边框,但我想在标签及其边框之间留出更多空间。
我怎样才能做到这一点?
我创建了一个简单的聊天应用程序,其中我们的消息在右侧(右对齐),所有其他消息在左侧(左对齐).我正在使用,NSAttributedString因为我大量修改文本的颜色等.每条消息都是UILabel.我的问题是,在正确对齐的消息的末尾,我想放一个空格,所以它看起来像这样:
"Some example sentence "
Run Code Online (Sandbox Code Playgroud)
而不是这样的:
"Some example sentece"
Run Code Online (Sandbox Code Playgroud)
每次我把空白放在那里它都被删除了(我也尝试了不间断的空间\u00a0,我得到了同样的问题(空格被删除)我的右对齐代码如下所示:
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.text /*attributes:attrDict*/];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentRight];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];
Run Code Online (Sandbox Code Playgroud)
后来我添加了一些其他属性与颜色等(没有任何改变文本本身).文本总是像最后一样用最后的空格:"Some example sentece "
最后我做这样的事情:
self.attributedText = attributedString;
Run Code Online (Sandbox Code Playgroud)
并且...我的空间被删除了.如何防止我的文本删除最后的空格?我需要那里.
编辑:
if (self.textAlignment == NSTextAlignmentRight) {
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setAlignment:NSTextAlignmentRight];
[paragraphStyle setTailIndent:0.1];
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [attributedString length])];
}
Run Code Online (Sandbox Code Playgroud)
这是我的tailIndent代码,它看起来像这样.在tailIndent之前,我在聊天的右侧有一条消息"test"(这里是左边因为我不知道如何正确对齐文本:P):
测试
在tailIndent之后:
Ť
那么会发生什么:文本从右到左,在这种情况下只留下最后一个字符.只有tailIndent 0.1!
whitespace objective-c nsattributedstring right-align nsmutableattributedstring
ios ×3
iphone ×3
objective-c ×2
uilabel ×2
autolayout ×1
fonts ×1
padding ×1
right-align ×1
swift ×1
whitespace ×1
xcode4 ×1