如何将 UIButton 左对齐,UILabel 右对齐。喜欢 instagram 的评论
How to align UIButton to the left with UILabel on the right. Like instagram's comments
我试图在右侧显示用户名,然后在按钮左侧显示带有一些文本的 UIlabel。
这是我所追求的一个例子:
我只是不确定如何获得约束来执行此操作,尤其是 UILabel 文本在按钮下方的下一行继续的部分...?!
如果有人有任何想法,我们将不胜感激!
此致。
我认为最好的方法是使用一个标签完成所有操作。您可以使用属性字符串(下面的代码)设置文本样式。然后创建一个UIButton放在label上面(UIButton的大小可以根据用户名长度调整)。
这是我在注册屏幕上创建免责声明标签时使用的方法,如下所示
下面是创建属性字符串的代码:
let disclaimerAttributedString = NSMutableAttributedString(string: disclaimerLabel.text!, attributes: [NSKernAttributeName: -0.4])
disclaimerAttributedString.addAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue], range: NSMakeRange(38, 12))
disclaimerAttributedString.addAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue], range: NSMakeRange(55, 14))
disclaimerLabel.attributedText = disclaimerAttributedString
我的情况略有不同,因为我将它应用于静态文本。对于您,您可以根据用户名的长度在代码中创建按钮,或者您可以通过将按钮固定到 UILabel 的左上角来近似它。
您可以通过使用
中的字符串扩展来计算用户名将占用的属性字符串的长度
请查看我在 UITextView idea 中的 UIButton 上创建的示例:
https://github.com/smozgur/UIButton-inside-UITextView我还在玩它,但到目前为止效果很好。
我试图在右侧显示用户名,然后在按钮左侧显示带有一些文本的 UIlabel。
这是我所追求的一个例子:
我只是不确定如何获得约束来执行此操作,尤其是 UILabel 文本在按钮下方的下一行继续的部分...?!
如果有人有任何想法,我们将不胜感激!
此致。
我认为最好的方法是使用一个标签完成所有操作。您可以使用属性字符串(下面的代码)设置文本样式。然后创建一个UIButton放在label上面(UIButton的大小可以根据用户名长度调整)。
这是我在注册屏幕上创建免责声明标签时使用的方法,如下所示
下面是创建属性字符串的代码:
let disclaimerAttributedString = NSMutableAttributedString(string: disclaimerLabel.text!, attributes: [NSKernAttributeName: -0.4])
disclaimerAttributedString.addAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue], range: NSMakeRange(38, 12))
disclaimerAttributedString.addAttributes([NSForegroundColorAttributeName: UIColor.whiteColor(), NSUnderlineStyleAttributeName: NSUnderlineStyle.StyleSingle.rawValue], range: NSMakeRange(55, 14))
disclaimerLabel.attributedText = disclaimerAttributedString
我的情况略有不同,因为我将它应用于静态文本。对于您,您可以根据用户名的长度在代码中创建按钮,或者您可以通过将按钮固定到 UILabel 的左上角来近似它。
您可以通过使用
请查看我在 UITextView idea 中的 UIButton 上创建的示例: https://github.com/smozgur/UIButton-inside-UITextView我还在玩它,但到目前为止效果很好。