Tho*_*mas 4 uiview uilabel ios swift
我正在尝试创建一个消息传递应用程序,并遇到一个非常奇怪的问题.
"托马斯"与文本泡沫底部之间存在如此大的空间的原因是因为UILabel它创造了另一条线.目前我使用的设置标签的文本attributedText属性,并传递NSMutableParagraphStyle了line spacing的8.如果我设置line spacing为0,"托马斯"和文本泡泡底部之间的空格会像这样消失:
这是它变得奇怪的地方.如果我将段落设置line spacing回来8,并在该行中添加几个字符,则会显示文本气泡,而不会显示额外的行:
非常感谢所有帮助:)
这是我的代码:
class MessageTableViewCell: UITableViewCell {
var didSetupConstraints = false
var thumbnail = UIImageView.newAutoLayoutView()
let messageTailIcon = UIImageView.newAutoLayoutView()
var messageView = UIView.newAutoLayoutView()
var messageLabel = UILabel.newAutoLayoutView()
override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
setupViews()
}
required init(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
func setupViews() {
thumbnail.image = UIImage(named: "ThomasBaldwin")
thumbnail.layer.cornerRadius = 17.5
thumbnail.clipsToBounds = true
messageTailIcon.image = UIImage(named: "MessageTailIcon")
messageView.backgroundColor = Application.greyColor
messageView.layer.cornerRadius = 10
let paragraphStyle = NSMutableParagraphStyle.new()
paragraphStyle.lineSpacing = 8
messageLabel.numberOfLines = 0
messageLabel.layer.cornerRadius = 10
messageLabel.attributedText = NSMutableAttributedString(
string: "Thomas says hello",
attributes: [
NSFontAttributeName: UIFont(name: "AvenirNextLTPro-Regular", size: 12.5)!,
NSForegroundColorAttributeName: UIColor.colorFromCode(0x262626),
NSBackgroundColorAttributeName: Application.greyColor,
NSKernAttributeName: 0.5,
NSParagraphStyleAttributeName: paragraphStyle
]
)
contentView.addSubview(thumbnail)
contentView.addSubview(messageView)
messageView.addSubview(messageTailIcon)
messageView.addSubview(messageLabel)
updateConstraints()
}
override func updateConstraints() {
if !didSetupConstraints {
thumbnail.autoPinEdgeToSuperviewEdge(.Top, withInset: 15)
thumbnail.autoPinEdgeToSuperviewEdge(.Leading, withInset: 8.5)
thumbnail.autoSetDimensionsToSize(CGSize(width: 35, height: 35))
messageView.autoPinEdgeToSuperviewEdge(.Top, withInset: 17.5)
messageView.autoPinEdge(.Leading, toEdge: .Trailing, ofView: thumbnail, withOffset: 10)
messageView.autoPinEdgeToSuperviewEdge(.Trailing, withInset: 24.5)
messageView.autoPinEdgeToSuperviewEdge(.Bottom)
messageTailIcon.autoPinEdgeToSuperviewEdge(.Top, withInset: 15)
messageTailIcon.autoPinEdgeToSuperviewEdge(.Leading, withInset: -10)
messageTailIcon.autoSetDimensionsToSize(CGSize(width: 18, height: 9))
messageLabel.autoPinEdgesToSuperviewEdgesWithInsets(UIEdgeInsets(top: 8.5, left: 10, bottom: 8.5, right: 5), excludingEdge: .Trailing)
messageLabel.autoPinEdgeToSuperviewEdge(.Trailing, withInset: 0, relation: .GreaterThanOrEqual)
didSetupConstraints = true
}
super.updateConstraints()
}
}
Run Code Online (Sandbox Code Playgroud)
如果您想查看演示该问题的示例项目,我已将其推送到github
好吧,所以最后锁定并轻松回答,这与你的KERNING仅归因于此.看这个:
同样无视红色单元格的大小,这在应用程序中不会发生这种情况,这只是我的截图大小不同的产品,但请亲自尝试一下.注释掉字距并重新应用它,你会看到同样的事情
用"托马斯"字距调整
没有"托马斯"的字谜
与"托马斯打招呼"的字谜
没有与"托马斯打招呼"字谜
我已经尽一切可能检查代码,使用不同的约束,我甚至玩了NSAttributedString的所有选项,唯一改变这种不良行为的是kerning属性,它对所有类型的字体都这样做,而不仅仅是Avenir.事实上,你在这个例子中使用的字体是系统字体,当你根本没有设置字体时,但我现在尝试使用3种字体,同样的效果,字距似乎被打破或者它的工作按预期对于Swift和/或ObjC,但我认为这实际上是一个错误.
大多数NSAttributedString选项,如果你想搞砸东西:
var myString1 = NSMutableAttributedString(string:"Thomas asdfadsf asdfasdfasdf asdfasdf asdfasdf \n asdfasdf asdf \n")
let myString1Font1 = UIFont.systemFontOfSize(12.0)
let originalNSString = myString1.string as NSString
let myString1Range1 = originalNSString.rangeOfString(myString1.string)
var myString1ParaStyle1 = NSMutableParagraphStyle()
myString1ParaStyle1.alignment = NSTextAlignment.Natural
myString1ParaStyle1.baseWritingDirection = NSWritingDirection.Natural
myString1ParaStyle1.defaultTabInterval = 0.0
myString1ParaStyle1.firstLineHeadIndent = 0.0
myString1ParaStyle1.headIndent = 0.0
myString1ParaStyle1.hyphenationFactor = 0.0
myString1ParaStyle1.lineBreakMode = NSLineBreakMode.ByWordWrapping
myString1ParaStyle1.lineHeightMultiple = 0.0
myString1ParaStyle1.lineSpacing = 8.0
myString1ParaStyle1.maximumLineHeight = 0.0
myString1ParaStyle1.minimumLineHeight = 0.0
myString1ParaStyle1.paragraphSpacing = 0.0
myString1ParaStyle1.paragraphSpacingBefore = 0.0
myString1ParaStyle1.tailIndent = 0.0
myString1.addAttribute(NSKernAttributeName, value:0.5, range:myString1Range1)
myString1.addAttribute(NSFontAttributeName, value:myString1Font1, range:myString1Range1)
myString1.addAttribute(NSParagraphStyleAttributeName, value:myString1ParaStyle1, range:myString1Range1)
myString1.addAttribute(NSBackgroundColorAttributeName, value:UIColor.redColor(), range:myString1Range1)
myString1.addAttribute(NSForegroundColorAttributeName, value:UIColor.blackColor(), range:myString1Range1)
Run Code Online (Sandbox Code Playgroud)
再次,这不是一个约束问题,我错了,这只是一个KERNING问题,这很糟糕,但这就是生活,也许这需要报告给RADAR.
此外,你可以自己尝试这个,但是你想要的任何东西都是0或0.00000或者你想要的零会产生错误的结果,我试过这个并且它会弄乱你的标签字段,就像字距调整会弄乱字段一样价值更高:
NSKernAttributeName: 0.00000000000001
Run Code Online (Sandbox Code Playgroud)
保持,我解决了它,从它看起来像,设置这个值,只需将它添加到您在示例项目中设置的paragraphStyle变量,它使用字距调整,不确定这是否适用于所有字体,但它至少修复了你的示例项目:
paragraphStyle.lineHeightMultiple = 1.5
Run Code Online (Sandbox Code Playgroud)
这种方法唯一的问题是它适用于有一个单词或一行的行,你必须根据新行出现的时间进行单词计数调整来设置这个"lineHeightMultiple",这很糟糕,但它有效,很明显,这不是一个非常好用的方法,但是如果你有一个内衬字符串,需要调整,如果你有更多,否则只需关闭字距,并且它将在没有这个行高度倍数的情况下解决.
就好像行高在内部变化并将字符推到一个新行,但apple并不会自动解决字符宽度的这种变化.
事实上,我认为你所寻找的答案根本不是跟踪,而是跟踪,这会使字母彼此分开.字距调整的问题在于用字体的字形进行字距调整并覆盖它们的一些行为,因此它可以像我们在这里看到的那样具有可感知的效果.
来自Apple:
preferredFontForTextStyle :,返回的特定字体包括根据用户首选项和上下文而变化的特征,包括跟踪(字母间距)调整,以及针对特定文本样式常量指定的用途进行调整.使用文本样式常量返回的字体旨在用于应用程序中除用户界面元素中的文本之外的所有文本,例如按钮,条形图和标签.当然,您需要选择在您的应用中看起来正确的文字样式.观察UIContentSizeCategoryDidChangeNotification也很重要,这样您就可以在用户更改内容大小类别时重新布局文本.当您的应用收到该通知时,它应该将invalidateIntrinsicContentSize消息发送到由Auto Layout定位的视图,或者将setNeedsLayout发送到手动定位的用户界面元素.它应该使首选字体或字体描述符无效并根据需要获取新字体.
如果你真的需要字距调整,那么如果字体有任何可用的话,你应该调整连字的字距值.
其他需要考虑的事情,这确实有效,但它也很大胆,所以它已经不是符合你上面风格的东西了,但它是你可以玩的东西:
let sytemDynamicFontDescriptor = UIFontDescriptor.preferredFontDescriptorWithTextStyle(UIFontTextStyleHeadline)
let size = sytemDynamicFontDescriptor.pointSize
let myString1Font1 = UIFont(descriptor: sytemDynamicFontDescriptor, size:size)
println(sytemDynamicFontDescriptor.fontAttributes())
messageLabel.numberOfLines = 0
messageLabel.layer.cornerRadius = 10
messageLabel.attributedText = NSMutableAttributedString(
string: "Thomas asdfad as ",
// string: "Thomas says hello", // switch back to this to see it display the text properly on one
attributes: [
NSFontAttributeName: myString1Font1,
NSForegroundColorAttributeName: UIColor.blackColor(),
NSBackgroundColorAttributeName: UIColor.redColor(),
NSKernAttributeName: 0.5,
NSParagraphStyleAttributeName: paragraphStyle
]
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3861 次 |
| 最近记录: |