Tôi muốn đặt Từ cụ thể có thể nhấp được trong văn bản UILabel
bằng Swift.Tạo nhãn có thể nhấp bằng Swift
Có thể không?
Nếu có nhiều nhãn ở đây thì làm cách nào để phát hiện từ nào được nhấn?
Tôi muốn đặt Từ cụ thể có thể nhấp được trong văn bản UILabel
bằng Swift.Tạo nhãn có thể nhấp bằng Swift
Có thể không?
Nếu có nhiều nhãn ở đây thì làm cách nào để phát hiện từ nào được nhấn?
Bạn không thể thực hiện với nhãn đơn giản.
Có thư viện trong github.
https://github.com/TTTAttributedLabel/TTTAttributedLabel
Từ đó bạn có thể sử dụng phương pháp gọi là yourLabel.addLinkToURL()
class ViewController: UIViewController , TTTAttributedLabelDelegate{
@IBOutlet var lbl: TTTAttributedLabel!
override func viewDidLoad() {
super.viewDidLoad()
var str : NSString = "Hello this is link"
lbl.delegate = self
lbl.text = str as String
var range : NSRange = str.rangeOfString("link")
lbl.addLinkToURL(NSURL(string: "http://github.com/mattt/")!, withRange: range)
}
func attributedLabel(label: TTTAttributedLabel!, didSelectLinkWithURL url: NSURL!) {
UIApplication.sharedApplication().openURL(url)
}
}
@ Daij-Djan Ok, thưa ngài. bạn có thể xin vui lòng gửi như là câu trả lời? Vì vậy, người hỏi sẽ nhận được kết quả chính xác và câu trả lời quá. –
Hãy thử. Tôi đã không làm điều này :) Ill nhìn vào nó ngay lập tức –
Tôi tin rằng giải pháp đơn giản nhất là sử dụng 1-2 nhãn và một nút. Autolayout làm cho nó thực sự dễ dàng. – Sulthan
SWIFT 3,0
privacyLabel.delegate = self
let strPolicy : NSString = "Agree to the Terms & Conditions"
privacyLabel.text = strPolicy as String
let range1 : NSRange = strPolicy.range(of: "Terms & Conditions")
privacyLabel.addLink(to: URL(string: "http://Terms.com")!, with: range1)
func attributedLabel(_ label: TTTAttributedLabel!, didSelectLinkWith url: URL!) {
print("url \(url)")
// UIApplication.sharedApplication().openURL(url)
}
Tôi muốn chia sẻ thư viện của tôi https://github.com/psharanda/Atributika
Nó chứa thay thế hiện đại của TTTAtributedLabel + bộ mạnh mẽ của các phương pháp để phát hiện và phong cách thứ khác nhau như thẻ, hashtags, đề cập đến vv (tất cả mọi thứ đó có thể nhấp chuột)
Một số mã để hiển thị nó như thế nào hoạt động:
let link = Style
.font(.boldSystemFont(ofSize: 14))
.foregroundColor(.black)
.foregroundColor(.red, .highlighted)
let tos = link.named("tos")
let pp = link.named("pp")
let all = Style
.font(.systemFont(ofSize: 14))
.foregroundColor(.gray)
let text = "<tos>Terms of Service</tos> and <pp>Privacy Policy</pp>"
.style(tags: tos, pp)
.styleAll(all)
let tosLabel = AttributedLabel()
tosLabel.textAlignment = .center
tosLabel.attributedText = text
tosLabel.onClick = { label, detection in
switch detection.type {
case .tag(let tag):
switch tag.name {
case "pp":
print("Privacy Policy clicked")
case "tos":
print("Terms of Service clicked")
default:
break
}
default:
break
}
}
view.addSubview(tosLabel)
thêm cử chỉ nhấn vào nhãn để bạn có thể nhận được điều này với hành động của nó. –