你如何将插图添加到 UILabel (iOS Swift)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32239457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you add inset to UILabel (iOS Swift)?
提问by andyjslee
I am an Android developer learning iOS. How do you add padding (inset) to UILabel in iOS Swift? I have not been successful at finding an easy tutorial on this. Thank you.
我是一名学习 iOS 的 Android 开发人员。你如何在 iOS Swift 中向 UILabel 添加填充(插图)?我没有成功找到一个简单的教程。谢谢你。
Also, how can I add margins to UI elements in Swift? When I try the below, nothing gets updated (upon hitting "add constraint").
另外,如何在 Swift 中为 UI 元素添加边距?当我尝试以下操作时,没有任何更新(点击“添加约束”后)。
回答by AndreasLukas
This is the code you need to add in Swift 3:
这是您需要在 Swift 3 中添加的代码:
class InsetLabel: UILabel {
let topInset = CGFloat(0)
let bottomInset = CGFloat(0)
let leftInset = CGFloat(20)
let rightInset = CGFloat(20)
override func drawText(in rect: CGRect) {
let insets: UIEdgeInsets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
override public var intrinsicContentSize: CGSize {
var intrinsicSuperViewContentSize = super.intrinsicContentSize
intrinsicSuperViewContentSize.height += topInset + bottomInset
intrinsicSuperViewContentSize.width += leftInset + rightInset
return intrinsicSuperViewContentSize
}
}
回答by Sebyddd
Probably the best idea is to subclass UILabel
and override drawTextInRect:
like this:
最好的想法可能是像这样子类化UILabel
和覆盖drawTextInRect:
:
class InsetLabel: UILabel {
override func drawTextInRect(rect: CGRect) {
super.drawTextInRect(UIEdgeInsetsInsetRect(rect, UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)))
}
}
SWIFT 4+:
斯威夫特 4+:
class InsetLabel: UILabel {
override func drawText(in rect: CGRect) {
super.drawText(in: rect.inset(by: UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)))
}
}
回答by Alexander Timonchev
need add this:
需要添加这个:
class InsetLabel: UILabel {
let topInset = CGFloat(12.0), bottomInset = CGFloat(12.0), leftInset = CGFloat(12.0), rightInset = CGFloat(12.0)
override func drawTextInRect(rect: CGRect) {
let insets: UIEdgeInsets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawTextInRect(UIEdgeInsetsInsetRect(rect, insets))
}
override func intrinsicContentSize() -> CGSize {
var intrinsicSuperViewContentSize = super.intrinsicContentSize()
intrinsicSuperViewContentSize.height += topInset + bottomInset
intrinsicSuperViewContentSize.width += leftInset + rightInset
return intrinsicSuperViewContentSize
}
}
回答by ClayJ
There is a solution simply using the storyboard. In the storyboard, create a UIView object whose purpose will be to surround your label in order to give it padding. Then, also from the storyboard, put your UILabel inside of that view. Now select the UILabel object and bring up the Add New Constraints popup by clicking the "Pin" icon. If you want a padding of 5 all the way around the UILabel, click each of the 4 red I-beams in the Add New Constraints popup, and THEN type 5 in each of the 4 boxes adjacent to the I-beams. If you type 5 first and then click an I-Beam, XCode reverts to whatever value was in the box before you typed 5. Finally click the Add Constraints button.
有一个简单的使用故事板的解决方案。在故事板中,创建一个 UIView 对象,其目的是围绕您的标签以便为其提供填充。然后,同样从情节提要中,将您的 UILabel 放在该视图中。现在选择 UILabel 对象并通过单击“Pin”图标打开 Add New Constraints 弹出窗口。如果您希望 UILabel 周围的填充为 5,请单击“添加新约束”弹出窗口中的 4 个红色工字梁中的每一个,然后在与工字梁相邻的 4 个框中键入 5。如果您先输入 5 然后单击 I-Beam,XCode 将恢复为您输入 5 之前框中的任何值。最后单击 Add Constraints 按钮。
In the Add Constraints popup, XCode ignores top, left, right, or bottom constraint values for which you have not clicked the corresponding red I-beam. This probably answers the second part of your question.
在 Add Constraints 弹出窗口中,XCode 会忽略您尚未单击相应红色 I 型梁的顶部、左侧、右侧或底部约束值。这可能回答了您问题的第二部分。
Remember also to setup the constraints of the UIView you added. If you don't define where it goes in the storyboard (e.g., pinned to the top and left of the main view of the view controller), it will not be assigned a default location, and it may not show up at all when you run your app.
还要记住设置您添加的 UIView 的约束。如果你没有定义它在故事板中的位置(例如,固定在视图控制器主视图的顶部和左侧),它不会被分配一个默认位置,当你运行您的应用程序。
回答by Jonathan
Another way would be to override textRect:forBounds:limitedToNumberOfLines:
另一种方法是覆盖 textRect:forBounds:limitedToNumberOfLines:
Returns the drawing rectangle for the label's text. Override this method in subclasses that require changes in the label's bounding rectangle to occur before the system performs other text layout calculations. Use the value in the numberOfLines parameter to limit the height of the returned rectangle to the specified number of lines of text. This method may be called by the system if there was a prior call to the sizeToFit() or sizeThatFits(_:) method. Note that labels in UITableViewCell objects are sized based on cell dimensions, and not on a requested size.
返回标签文本的绘图矩形。在需要在系统执行其他文本布局计算之前更改标签的边界矩形的子类中覆盖此方法。使用 numberOfLines 参数中的值将返回的矩形的高度限制为指定的文本行数。如果先前调用了 sizeToFit() 或 sizeThatFits(_:) 方法,则系统可能会调用此方法。请注意, UITableViewCell 对象中的标签的大小是基于单元格尺寸的,而不是请求的大小。
public class JPSLabel : UILabel
{
open var textInsets: UIEdgeInsets = UIEdgeInsets.zero
override public func textRect(forBounds bounds: CGRect, limitedToNumberOfLines numberOfLines: Int) -> CGRect
{
var insetBounds = super.textRect(forBounds: bounds, limitedToNumberOfLines: numberOfLines)
insetBounds.size.width += self.textInsets.left + self.textInsets.right
insetBounds.size.height += self.textInsets.top + self.textInsets.bottom
return insetBounds
}
}
回答by divya
I have tried with it on Swift 4.2, hopefully, it works for you!
我已经在 Swift 4.2 上尝试过,希望它对你有用!
@IBDesignable class PaddingLabel: UILabel {
@IBInspectable var topInset: CGFloat = 5.0
@IBInspectable var bottomInset: CGFloat = 5.0
@IBInspectable var leftInset: CGFloat = 7.0
@IBInspectable var rightInset: CGFloat = 7.0
override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets.init(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawText(in: rect.inset(by: insets))
}
override var intrinsicContentSize: CGSize {
let size = super.intrinsicContentSize
return CGSize(width: size.width + leftInset + rightInset,
height: size.height + topInset + bottomInset)
}
}
Or
或者
you can use CocoaPods here https://github.com/levantAJ/PaddingLabel
你可以在这里使用 CocoaPods https://github.com/levantAJ/PaddingLabel
pod 'PaddingLabel', '1.1'
pod 'PaddingLabel', '1.1'