最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]swift中如何控制Label中字体与边框的间距

Swift crifan 4388浏览 0评论

想要实现:

即,label文字的上下和左右,都和边框间距8px=4point

swift uilabel text padding

ios – Adding space/padding to a UILabel (Swift) – Stack Overflow

How do you add inset to UILabel (iOS Swift)? – Stack Overflow

ios – Inset text in UILabel from left – Stack Overflow

最后换成TextView,然后加上textContainerInset即可:

                self.systemNoticeTextView = UITextView()
               
                self.systemNoticeTextView.text = notifMsg.notice
                self.systemNoticeTextView.textAlignment = NSTextAlignment.Left
                self.systemNoticeTextView.textColor = ColorSystemNoticeText
                self.systemNoticeTextView.font = FontSystemNotice
                self.systemNoticeTextView.backgroundColor = ColorSystemNoticeBackground
                self.systemNoticeTextView.layer.borderWidth = 1
                self.systemNoticeTextView.layer.borderColor = ColorSystemNoticeBackground.CGColor
                self.systemNoticeTextView.layer.cornerRadius = 2
               
                self.systemNoticeTextView.editable = false
                self.systemNoticeTextView.selectable = false
                self.systemNoticeTextView.scrollEnabled = false
               
                self.systemNoticeTextView.textContainerInset = InsetSystemNotice
               
                self.contentView.addSubview(self.systemNoticeTextView)
               
                constrain(systemNoticeTextView) {systemNoticeLabel in
                    systemNoticeLabel.top == systemNoticeLabel.superview!.top + PaddingYMessageTVC
                    systemNoticeLabel.centerX == systemNoticeLabel.superview!.centerX
                    systemNoticeLabel.bottom == systemNoticeLabel.superview!.bottom
                    systemNoticeLabel.width <= systemNoticeLabel.superview!.width (2 * (WidthAvatarPadding + SizeAvatarImage + BubbleArrowWidth))
                }

效果:

转载请注明:在路上 » [已解决]swift中如何控制Label中字体与边框的间距

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
84 queries in 0.190 seconds, using 22.10MB memory