Uiview and view of IOS learning diary (I) label uilabel

Mobile terminal development_ Xiaohei 2022-06-23 17:03:38 阅读数:327

uiviewviewioslearningdiary

Keep creating , Accelerate growth ! This is my participation 「 Nuggets day new plan · 6 Yuegengwen challenge 」 Of the 22 God , Click to see the event details

Hello everyone , I'm Xiaohei , A programmer who's not bald yet ~~~

From today , We will turn to sharing UIView Learning records of views and controls ,SwiftUI You can read previous articles about the sharing and actual combat source code of , I only learn pure code iOS Development , Therefore, only the introduction of pure code using components will appear in the subsequent articles , There will be no use of storyboards , Don't talk much , Text begins : This time we introduce UILabel Components , Use as a text label , The effect is as shown in the picture :

image.png

UILabel

UILable It is a view that can display one or more lines of information text .

You can configure the overall appearance of the label text , And use strings with attributes to customize the appearance of substrings in the text . Add and customize labels in the interface programmatically or using the property inspector in the interface builder .

The programming method is pure code development Or you can configure properties through the property inspector in the storyboard

Follow these steps to add labels :

  1. Definition UILabel Variable
  2. Define the position, width and height
  3. Define content and other styles
  4. Add to parent component
attribute explain
text Display the text of
attributedText The style text displayed by the label
font The font of the text
textColor The color of the text
textAlignment Alignment of text
lineBreakMode How to wrap and truncate label text
isEnable Determines whether the label draws its text when enabled
enablesMarqueeWhenAncestorFocused Used to determine whether a label scrolls its text when one of the contained views has focus

There are several ways to truncate text :

The way explain
byWordWrapping Line breaks occur at word boundaries , Unless words can't be placed on a line
byCharWrapping Newline occurs before the first inappropriate character
byClipping The line does not extend beyond the edge of the text container
byTruncatingHead The ellipsis indicates the missing text at the beginning of the line
byTruncatingTail The ellipsis symbol indicates the missing text at the end of the line
byTruncatingMiddle The ellipsis symbol indicates the missing text in the middle of the line

The specific code is as follows :

 let screen = UIScreen.main.bounds
let labelWidth:CGFloat = 150
let labelHeight:CGFloat = 50
let labelTop:CGFloat = titleBarHeight+20
let label=UILabel(frame: CGRect(x: (screen.size.width-labelWidth)/2, y: labelTop, width: labelWidth, height: labelHeight))
label.text = " I am a UILabel"
label.attributedText = NSAttributedString(string: " I am style text ")
label.font = .boldSystemFont(ofSize: 25)
label.textColor = .orange
label.textAlignment = .center
label.lineBreakMode = .byTruncatingTail
self.view.addSubview(label)

The above is IOS About China UILabel Use , Today's UIView That's all for learning and sharing , I will continue to output iOS Development Notes , Thanks for reading ! Joint efforts ! Everybody good night !

版权声明:本文为[Mobile terminal development_ Xiaohei]所创,转载请带上原文链接,感谢。 https://qdmana.com/2022/174/202206231602189455.html