有没有办法可以围绕每个单词绘制边框UILabel
.假设UILabel
包含字符串"This is the Line 1".
我想要5个不同的边框,大约5个字
这个
是
该
线
1
VikingoS say.. 7
我不知道UILabel的易用代码,但对于UITextView:
斯威夫特操场
建立:
import UIKit let string = "Lorem ipsum dolor sit amet" let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) textView.text = string
使用正则表达式获得每个单词的匹配:
let pattern = "[a-zA-Z0-9]+" let regex = try! NSRegularExpression(pattern: pattern, options: []) let matches = regex.matchesInString(string, options: [], range: NSMakeRange(0, string.characters.count))
函数获取每个匹配的矩形(从此答案移植):
func frameOfTextInRange(range:NSRange, inTextView textView:UITextView) -> CGRect { let beginning = textView.beginningOfDocument let start = textView.positionFromPosition(beginning, offset: range.location)! let end = textView.positionFromPosition(start, offset: range.length)! let textRange = textView.textRangeFromPosition(start, toPosition: end)! let rect = textView.firstRectForRange(textRange) return textView.convertRect(rect, fromView: textView) }
迭代每个匹配,获取其框架,使用它来创建背景视图,将其添加到文本视图:
for m in matches { let range = m.range let frame = frameOfTextInRange(range, inTextView: textView) let v = UIView(frame: frame) v.layer.borderWidth = 1 v.layer.borderColor = UIColor.blueColor().CGColor textView.addSubview(v) }
但这可能不会给你期待的结果.要获得更好的控件,可以使用属性字符串.
这里使用属性字符串的相同代码
import UIKit let string = "Lorem ipsum dolor sit amet" let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) let attributedString = NSMutableAttributedString(string: string) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineHeightMultiple = 1.25 attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, string.characters.count)) textView.attributedText = attributedString let pattern = "[a-zA-Z0-9]+" let regex = try! NSRegularExpression(pattern: pattern, options: []) let matches = regex.matchesInString(string, options: [], range: NSMakeRange(0, string.characters.count)) func frameOfTextInRange(range:NSRange, inTextView textView:UITextView) -> CGRect { let beginning = textView.beginningOfDocument let start = textView.positionFromPosition(beginning, offset: range.location)! let end = textView.positionFromPosition(start, offset: range.length)! let textRange = textView.textRangeFromPosition(start, toPosition: end)! let rect = textView.firstRectForRange(textRange) return textView.convertRect(rect, fromView: textView) } for m in matches { let range = m.range var frame = frameOfTextInRange(range, inTextView: textView) frame = CGRectInset(frame, CGFloat(-1.2), CGFloat(2)) frame = CGRectOffset(frame, CGFloat(0), CGFloat(2)) let v = UIView(frame: frame) v.layer.borderWidth = 1 v.layer.borderColor = UIColor.blueColor().CGColor textView.addSubview(v) }
创建漂亮样式也很有帮助,可以将视图添加到背景视图中,并将该textview添加到顶部
import UIKit let string = "Lorem ipsum dolor sit amet" let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) let textViewBG = UIView(frame: textView.bounds) textViewBG.backgroundColor = UIColor.whiteColor() let attributedString = NSMutableAttributedString(string: string) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineHeightMultiple = 1.25 attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, string.characters.count)) attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, string.characters.count)) textView.attributedText = attributedString textView.backgroundColor = UIColor.clearColor() let pattern = "[a-zA-Z0-9]+" let regex = try! NSRegularExpression(pattern: pattern, options: []) let matches = regex.matchesInString(string, options: [], range: NSMakeRange(0, string.characters.count)) func frameOfTextInRange(range:NSRange, inTextView textView:UITextView) -> CGRect { let beginning = textView.beginningOfDocument let start = textView.positionFromPosition(beginning, offset: range.location)! let end = textView.positionFromPosition(start, offset: range.length)! let textRange = textView.textRangeFromPosition(start, toPosition: end)! let rect = textView.firstRectForRange(textRange) return textView.convertRect(rect, fromView: textView) } for m in matches { let range = m.range var frame = frameOfTextInRange(range, inTextView: textView) frame = CGRectInset(frame, CGFloat(-1.2), CGFloat(2)) frame = CGRectOffset(frame, CGFloat(0), CGFloat(2)) let v = UIView(frame: frame) v.layer.cornerRadius = 2 v.backgroundColor = UIColor(hue: 0.66, saturation: 0.6, brightness: 1, alpha: 1) textViewBG.addSubview(v) } textViewBG.addSubview(textView)
为了增加单词之间的空间,我们可以改变空格的字距
import UIKit func frameOfTextInRange(range:NSRange, inTextView textView:UITextView) -> CGRect { let beginning = textView.beginningOfDocument let start = textView.positionFromPosition(beginning, offset: range.location)! let end = textView.positionFromPosition(start, offset: range.length)! let textRange = textView.textRangeFromPosition(start, toPosition: end)! let rect = textView.firstRectForRange(textRange) return textView.convertRect(rect, fromView: textView) } let string = "Lorem ipsum dolor sit amet" let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) textView.backgroundColor = UIColor.clearColor() textView.attributedText = { let attributedString = NSMutableAttributedString(string: string) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineHeightMultiple = 1.25 attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, string.characters.count)) attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, string.characters.count)) let regex = try! NSRegularExpression(pattern: "\\s", options: []) let matches = regex.matchesInString(string, options: [], range: NSMakeRange(0, string.characters.count)) for m in matches { attributedString.addAttribute(NSKernAttributeName, value: 6, range: m.range) } return NSAttributedString(attributedString: attributedString) }() let textViewBG = UIView(frame: textView.bounds) textViewBG.backgroundColor = UIColor.whiteColor() let pattern = "[^ ]+" let regex = try! NSRegularExpression(pattern: pattern, options: []) let matches = regex.matchesInString(string, options: [], range: NSMakeRange(0, string.characters.count)) for m in matches { textViewBG.addSubview({ let range = m.range var frame = frameOfTextInRange(range, inTextView: textView) frame = CGRectInset(frame, CGFloat(-3), CGFloat(2)) frame = CGRectOffset(frame, CGFloat(0), CGFloat(3)) let v = UIView(frame: frame) v.layer.cornerRadius = 2 v.backgroundColor = UIColor(hue: 211.0/360.0, saturation: 0.35, brightness: 0.78 , alpha: 1) return v }()) } textViewBG.addSubview(textView)
我不知道UILabel的易用代码,但对于UITextView:
斯威夫特操场
建立:
import UIKit let string = "Lorem ipsum dolor sit amet" let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) textView.text = string
使用正则表达式获得每个单词的匹配:
let pattern = "[a-zA-Z0-9]+" let regex = try! NSRegularExpression(pattern: pattern, options: []) let matches = regex.matchesInString(string, options: [], range: NSMakeRange(0, string.characters.count))
函数获取每个匹配的矩形(从此答案移植):
func frameOfTextInRange(range:NSRange, inTextView textView:UITextView) -> CGRect { let beginning = textView.beginningOfDocument let start = textView.positionFromPosition(beginning, offset: range.location)! let end = textView.positionFromPosition(start, offset: range.length)! let textRange = textView.textRangeFromPosition(start, toPosition: end)! let rect = textView.firstRectForRange(textRange) return textView.convertRect(rect, fromView: textView) }
迭代每个匹配,获取其框架,使用它来创建背景视图,将其添加到文本视图:
for m in matches { let range = m.range let frame = frameOfTextInRange(range, inTextView: textView) let v = UIView(frame: frame) v.layer.borderWidth = 1 v.layer.borderColor = UIColor.blueColor().CGColor textView.addSubview(v) }
但这可能不会给你期待的结果.要获得更好的控件,可以使用属性字符串.
这里使用属性字符串的相同代码
import UIKit let string = "Lorem ipsum dolor sit amet" let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) let attributedString = NSMutableAttributedString(string: string) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineHeightMultiple = 1.25 attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, string.characters.count)) textView.attributedText = attributedString let pattern = "[a-zA-Z0-9]+" let regex = try! NSRegularExpression(pattern: pattern, options: []) let matches = regex.matchesInString(string, options: [], range: NSMakeRange(0, string.characters.count)) func frameOfTextInRange(range:NSRange, inTextView textView:UITextView) -> CGRect { let beginning = textView.beginningOfDocument let start = textView.positionFromPosition(beginning, offset: range.location)! let end = textView.positionFromPosition(start, offset: range.length)! let textRange = textView.textRangeFromPosition(start, toPosition: end)! let rect = textView.firstRectForRange(textRange) return textView.convertRect(rect, fromView: textView) } for m in matches { let range = m.range var frame = frameOfTextInRange(range, inTextView: textView) frame = CGRectInset(frame, CGFloat(-1.2), CGFloat(2)) frame = CGRectOffset(frame, CGFloat(0), CGFloat(2)) let v = UIView(frame: frame) v.layer.borderWidth = 1 v.layer.borderColor = UIColor.blueColor().CGColor textView.addSubview(v) }
创建漂亮样式也很有帮助,可以将视图添加到背景视图中,并将该textview添加到顶部
import UIKit let string = "Lorem ipsum dolor sit amet" let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) let textViewBG = UIView(frame: textView.bounds) textViewBG.backgroundColor = UIColor.whiteColor() let attributedString = NSMutableAttributedString(string: string) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineHeightMultiple = 1.25 attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, string.characters.count)) attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, string.characters.count)) textView.attributedText = attributedString textView.backgroundColor = UIColor.clearColor() let pattern = "[a-zA-Z0-9]+" let regex = try! NSRegularExpression(pattern: pattern, options: []) let matches = regex.matchesInString(string, options: [], range: NSMakeRange(0, string.characters.count)) func frameOfTextInRange(range:NSRange, inTextView textView:UITextView) -> CGRect { let beginning = textView.beginningOfDocument let start = textView.positionFromPosition(beginning, offset: range.location)! let end = textView.positionFromPosition(start, offset: range.length)! let textRange = textView.textRangeFromPosition(start, toPosition: end)! let rect = textView.firstRectForRange(textRange) return textView.convertRect(rect, fromView: textView) } for m in matches { let range = m.range var frame = frameOfTextInRange(range, inTextView: textView) frame = CGRectInset(frame, CGFloat(-1.2), CGFloat(2)) frame = CGRectOffset(frame, CGFloat(0), CGFloat(2)) let v = UIView(frame: frame) v.layer.cornerRadius = 2 v.backgroundColor = UIColor(hue: 0.66, saturation: 0.6, brightness: 1, alpha: 1) textViewBG.addSubview(v) } textViewBG.addSubview(textView)
为了增加单词之间的空间,我们可以改变空格的字距
import UIKit func frameOfTextInRange(range:NSRange, inTextView textView:UITextView) -> CGRect { let beginning = textView.beginningOfDocument let start = textView.positionFromPosition(beginning, offset: range.location)! let end = textView.positionFromPosition(start, offset: range.length)! let textRange = textView.textRangeFromPosition(start, toPosition: end)! let rect = textView.firstRectForRange(textRange) return textView.convertRect(rect, fromView: textView) } let string = "Lorem ipsum dolor sit amet" let textView = UITextView(frame: CGRect(x: 0, y: 0, width: 100, height: 100)) textView.backgroundColor = UIColor.clearColor() textView.attributedText = { let attributedString = NSMutableAttributedString(string: string) let paragraphStyle = NSMutableParagraphStyle() paragraphStyle.lineHeightMultiple = 1.25 attributedString.addAttribute(NSParagraphStyleAttributeName, value: paragraphStyle, range: NSMakeRange(0, string.characters.count)) attributedString.addAttribute(NSForegroundColorAttributeName, value: UIColor.whiteColor(), range: NSMakeRange(0, string.characters.count)) let regex = try! NSRegularExpression(pattern: "\\s", options: []) let matches = regex.matchesInString(string, options: [], range: NSMakeRange(0, string.characters.count)) for m in matches { attributedString.addAttribute(NSKernAttributeName, value: 6, range: m.range) } return NSAttributedString(attributedString: attributedString) }() let textViewBG = UIView(frame: textView.bounds) textViewBG.backgroundColor = UIColor.whiteColor() let pattern = "[^ ]+" let regex = try! NSRegularExpression(pattern: pattern, options: []) let matches = regex.matchesInString(string, options: [], range: NSMakeRange(0, string.characters.count)) for m in matches { textViewBG.addSubview({ let range = m.range var frame = frameOfTextInRange(range, inTextView: textView) frame = CGRectInset(frame, CGFloat(-3), CGFloat(2)) frame = CGRectOffset(frame, CGFloat(0), CGFloat(3)) let v = UIView(frame: frame) v.layer.cornerRadius = 2 v.backgroundColor = UIColor(hue: 211.0/360.0, saturation: 0.35, brightness: 0.78 , alpha: 1) return v }()) } textViewBG.addSubview(textView)