uilabel

Text not vertically centered in UILabel

人走茶凉 提交于 2020-05-24 09:36:28
问题 I've created a Label with the following code : func setupValueLabel() { valueLabel.numberOfLines = 1 valueLabel.font = UIFont(name: "Avenir-Black", size: 50) valueLabel.adjustsFontSizeToFitWidth = true valueLabel.clipsToBounds = true valueLabel.backgroundColor = UIColor.greenColor() valueLabel.textColor = valuesColor valueLabel.textAlignment = NSTextAlignment.Center } I don't really understand why but the label is not vertically centered : Do I have to do anything specific so it can be

How to get the height of a UILabel in Swift?

纵饮孤独 提交于 2020-05-23 05:51:48
问题 I am a beginner in Swift and I am trying to get the height of a label. The label has multiple lines of text. I want to know the total height it occupies on the screen. 回答1: it's simple, just call label.bounds.size.height 回答2: Swift 4 with extension extension UILabel{ public var requiredHeight: CGFloat { let label = UILabel(frame: CGRect(x: 0, y: 0, width: frame.width, height: CGFloat.greatestFiniteMagnitude)) label.numberOfLines = 0 label.lineBreakMode = NSLineBreakMode.byWordWrapping label

How to get the height of a UILabel in Swift?

依然范特西╮ 提交于 2020-05-23 05:51:13
问题 I am a beginner in Swift and I am trying to get the height of a label. The label has multiple lines of text. I want to know the total height it occupies on the screen. 回答1: it's simple, just call label.bounds.size.height 回答2: Swift 4 with extension extension UILabel{ public var requiredHeight: CGFloat { let label = UILabel(frame: CGRect(x: 0, y: 0, width: frame.width, height: CGFloat.greatestFiniteMagnitude)) label.numberOfLines = 0 label.lineBreakMode = NSLineBreakMode.byWordWrapping label

How to draw vertical text in UILabel

丶灬走出姿态 提交于 2020-05-22 11:26:46
问题 I'm currently working on drawing vertical Chinese text in a label. Here's what I am trying to achieve, albeit with Chinese Characters: I've been planning to draw each character, rotate each character 90 degrees to the left, then rotating the entire label via affine transformations to get the final result. However, it feels awfully complicated. Is there an easier way to draw the text without complicated CoreGraphics magic that I'm missing? 回答1: Well, You can do like below: labelObject

How to remove bottom padding of UILabel with attributedText inside UIStackView

拜拜、爱过 提交于 2020-05-17 06:18:12
问题 I want to remove the bottom padding of a UILabel with attributedText inside a UIStackview. I found this solution How to remove the extra padding below an one line UILabel. This works with normal text but not with attributed text. let textLabel = UILabel() textLabel.translatesAutoresizingMaskIntoConstraints = false textLabel.text = "What is a chemical property and how can you observe it?" textLabel.numberOfLines = 0 textLabel.lineBreakMode = .byWordWrapping textLabel.backgroundColor =

UILabel with layer Corner radius AND shadow

情到浓时终转凉″ 提交于 2020-05-16 01:51:07
问题 i'm looking for a way to display a UILabel with layer.cornerRadius and layer.shadow . I figured out, that with label.clipsToBounds = true the cornerRadius will be set and with label.masksToBounds = false the shadow will be displayed With both only the shadow, without the cornerRadius will be displayed let label = UILabel() label.textAlignment = .center label.font = UIFont.systemFont(ofSize: 32, weight: .regular) label.textColor = .white label.clipsToBounds = true label.backgroundColor =

How can I get weight of a UILabel?

故事扮演 提交于 2020-05-14 20:05:07
问题 User from storyboard or programatically can set font weight as regular, semi bold etc. I want to read weight for any font label. I tried po label.font.description and font-weight is there and but there is no exposed variable to get weight from font. Is it possible? 回答1: To get the font weight string name, use the font descriptor and pass in the face attribute. Swift 4.2 let font = UIFont.systemFont(ofSize: 14, weight: UIFont.Weight.bold) let face = font.fontDescriptor.object(forKey:

centering text in a UILabel with an NSAttributedString

雨燕双飞 提交于 2020-05-09 19:24:12
问题 Going through some basic improvements to a application I am working on. Still new to the iOS swift development scene. I figured that the lines of text in my code would automatically be centered because I set the label to center. After a little bit of research I discovered this is not the case. How would I align code like this to center: let atrString = try NSAttributedString( data: assetDetails!.cardDescription.dataUsingEncoding(NSUTF8StringEncoding)!, options:

How to check if UILabel has attributedText or normal text programmatically?

只愿长相守 提交于 2020-04-10 08:11:54
问题 Is there any way to tell if UILabel has its text set using label.attributedText or label.text property? The problem is when you set attributedText , text is also updated and vice versa, so it is not possible to check these properties for nil. 回答1: This is what I use. If range length equals the unattributed text length then the text only has a single attribute and is therefore unattributed. NSRange range; [label.attributedText attributesAtIndex:0 effectiveRange:&range]; BOOL isAttributed =

How to check if UILabel has attributedText or normal text programmatically?

时光怂恿深爱的人放手 提交于 2020-04-10 08:11:08
问题 Is there any way to tell if UILabel has its text set using label.attributedText or label.text property? The problem is when you set attributedText , text is also updated and vice versa, so it is not possible to check these properties for nil. 回答1: This is what I use. If range length equals the unattributed text length then the text only has a single attribute and is therefore unattributed. NSRange range; [label.attributedText attributesAtIndex:0 effectiveRange:&range]; BOOL isAttributed =