나는이 UITextView
을 표시하는가 NSAttributedString
. 이 문자열에는 탭할 수있게 만들고 싶은 단어가 포함되어 있으므로 탭하면 다시 전화를 걸어 작업을 수행 할 수 있습니다. 나는 그것을 깨닫는다UITextView
URL에 대한 탭을 감지하고 대리인에게 전화를 걸 수 있지만 URL이 아닙니다.
iOS 7과 TextKit의 힘으로 이제 가능할 것 같지만 예제를 찾을 수 없으며 어디서 시작해야할지 모르겠습니다.
이제 문자열에 사용자 지정 속성을 만들 수 있다는 것을 알고 있으며 (아직 수행하지는 않았지만) 마법 단어 중 하나가 탭되었는지 감지하는 데 유용 할 것입니다. 어쨌든 나는 여전히 그 탭을 가로 채고 어떤 단어에서 탭이 발생했는지 감지하는 방법을 모릅니다.
iOS 6 호환성은 필요 하지 않습니다.
답변
나는 단지 다른 사람들을 조금 더 돕고 싶었습니다. Shmidt의 답변에 이어 원래 질문에서 요청한대로 정확하게 수행 할 수 있습니다.
1) 클릭 가능한 단어에 적용되는 사용자 정의 속성으로 속성 문자열을 만듭니다. 예.
NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:@"a clickable word" attributes:@{ @"myCustomTag" : @(YES) }];
[paragraph appendAttributedString:attributedString];
2) 해당 문자열을 표시 할 UITextView를 만들고 여기에 UITapGestureRecognizer를 추가합니다. 그런 다음 탭 처리 :
- (void)textTapped:(UITapGestureRecognizer *)recognizer
{
UITextView *textView = (UITextView *)recognizer.view;
// Location of the tap in text-container coordinates
NSLayoutManager *layoutManager = textView.layoutManager;
CGPoint location = [recognizer locationInView:textView];
location.x -= textView.textContainerInset.left;
location.y -= textView.textContainerInset.top;
// Find the character that's been tapped on
NSUInteger characterIndex;
characterIndex = [layoutManager characterIndexForPoint:location
inTextContainer:textView.textContainer
fractionOfDistanceBetweenInsertionPoints:NULL];
if (characterIndex < textView.textStorage.length) {
NSRange range;
id value = [textView.attributedText attribute:@"myCustomTag" atIndex:characterIndex effectiveRange:&range];
// Handle as required...
NSLog(@"%@, %d, %d", value, range.location, range.length);
}
}
방법을 알면 아주 쉽습니다!
답변
Swift로 속성 텍스트에 대한 탭 감지
때때로 초보자에게는 설정을하는 방법을 알기가 조금 어렵습니다 (어쨌든 저를위한 것이 었습니다). 그래서이 예제는 조금 더 꽉 차 있습니다.
UITextView
프로젝트에를 추가 하십시오.
콘센트
연결 UITextView
받는 사람을 ViewController
라는 콘센트 textView
.
맞춤 속성
Extension 을 만들어 사용자 지정 속성을 만들 것 입니다.
참고 : 이 단계는 기술적으로 선택 사항이지만 그렇게하지 않으면 다음 부분에서 코드를 편집하여와 같은 표준 속성을 사용해야합니다
NSAttributedString.Key.foregroundColor
. 사용자 지정 특성 사용의 장점은 특성이있는 텍스트 범위에 저장할 값을 정의 할 수 있다는 것입니다.
다음을 사용하여 새 신속한 파일 추가 File> New> File …> iOS> Source> Swift File . 원하는대로 부를 수 있습니다. 내 NSAttributedStringKey + CustomAttribute.swift를 호출하고 있습니다. 있습니다.
다음 코드를 붙여 넣으십시오.
import Foundation
extension NSAttributedString.Key {
static let myAttributeName = NSAttributedString.Key(rawValue: "MyCustomAttribute")
}
암호
ViewController.swift의 코드를 다음으로 바꿉니다. 를 참고 UIGestureRecognizerDelegate
.
import UIKit
class ViewController: UIViewController, UIGestureRecognizerDelegate {
@IBOutlet weak var textView: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
// Create an attributed string
let myString = NSMutableAttributedString(string: "Swift attributed text")
// Set an attribute on part of the string
let myRange = NSRange(location: 0, length: 5) // range of "Swift"
let myCustomAttribute = [ NSAttributedString.Key.myAttributeName: "some value"]
myString.addAttributes(myCustomAttribute, range: myRange)
textView.attributedText = myString
// Add tap gesture recognizer to Text View
let tap = UITapGestureRecognizer(target: self, action: #selector(myMethodToHandleTap(_:)))
tap.delegate = self
textView.addGestureRecognizer(tap)
}
@objc func myMethodToHandleTap(_ sender: UITapGestureRecognizer) {
let myTextView = sender.view as! UITextView
let layoutManager = myTextView.layoutManager
// location of tap in myTextView coordinates and taking the inset into account
var location = sender.location(in: myTextView)
location.x -= myTextView.textContainerInset.left;
location.y -= myTextView.textContainerInset.top;
// character index at tap location
let characterIndex = layoutManager.characterIndex(for: location, in: myTextView.textContainer, fractionOfDistanceBetweenInsertionPoints: nil)
// if index is valid then do something.
if characterIndex < myTextView.textStorage.length {
// print the character index
print("character index: \(characterIndex)")
// print the character at the index
let myRange = NSRange(location: characterIndex, length: 1)
let substring = (myTextView.attributedText.string as NSString).substring(with: myRange)
print("character at index: \(substring)")
// check if the tap location has a certain attribute
let attributeName = NSAttributedString.Key.myAttributeName
let attributeValue = myTextView.attributedText?.attribute(attributeName, at: characterIndex, effectiveRange: nil)
if let value = attributeValue {
print("You tapped on \(attributeName.rawValue) and the value is: \(value)")
}
}
}
}
이제 “Swift”의 “w”를 탭하면 다음 결과가 표시됩니다.
character index: 1
character at index: w
You tapped on MyCustomAttribute and the value is: some value
노트
- 여기에서는 사용자 지정 속성을 사용했지만
NSAttributedString.Key.foregroundColor
값이UIColor.green
. - 이전에는 텍스트보기를 편집하거나 선택할 수 없었지만 Swift 4.2에 대한 업데이트 된 답변에서는 선택 여부에 관계없이 잘 작동하는 것 같습니다.
추가 연구
이 답변은이 질문에 대한 몇 가지 다른 답변을 기반으로합니다. 이 외에도 참조
답변
이것은 @tarmes 답변을 기반으로 약간 수정 된 버전입니다. 아래의 조정 없이는 value
아무것도 반환 할 변수를 얻을 수 없습니다 null
. 또한 결과 작업을 결정하기 위해 반환 된 전체 속성 사전이 필요했습니다. 나는 이것을 코멘트에 넣었을 것이지만 그렇게 할 담당자가없는 것 같습니다. 프로토콜을 위반 한 경우 미리 사과드립니다.
특정 조정은 textView.textStorage
대신 사용하는 것입니다.textView.attributedText
. 아직 iOS 프로그래머를 배우고있는 저는 이것이 왜 그런지 잘 모르겠지만 아마도 다른 누군가가 우리를 깨달을 수있을 것입니다.
탭 핸들링 방법의 특정 수정 :
NSDictionary *attributesOfTappedText = [textView.textStorage attributesAtIndex:characterIndex effectiveRange:&range];
내 뷰 컨트롤러의 전체 코드
- (void)viewDidLoad
{
[super viewDidLoad];
self.textView.attributedText = [self attributedTextViewString];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(textTapped:)];
[self.textView addGestureRecognizer:tap];
}
- (NSAttributedString *)attributedTextViewString
{
NSMutableAttributedString *paragraph = [[NSMutableAttributedString alloc] initWithString:@"This is a string with " attributes:@{NSForegroundColorAttributeName:[UIColor blueColor]}];
NSAttributedString* attributedString = [[NSAttributedString alloc] initWithString:@"a tappable string"
attributes:@{@"tappable":@(YES),
@"networkCallRequired": @(YES),
@"loadCatPicture": @(NO)}];
NSAttributedString* anotherAttributedString = [[NSAttributedString alloc] initWithString:@" and another tappable string"
attributes:@{@"tappable":@(YES),
@"networkCallRequired": @(NO),
@"loadCatPicture": @(YES)}];
[paragraph appendAttributedString:attributedString];
[paragraph appendAttributedString:anotherAttributedString];
return [paragraph copy];
}
- (void)textTapped:(UITapGestureRecognizer *)recognizer
{
UITextView *textView = (UITextView *)recognizer.view;
// Location of the tap in text-container coordinates
NSLayoutManager *layoutManager = textView.layoutManager;
CGPoint location = [recognizer locationInView:textView];
location.x -= textView.textContainerInset.left;
location.y -= textView.textContainerInset.top;
NSLog(@"location: %@", NSStringFromCGPoint(location));
// Find the character that's been tapped on
NSUInteger characterIndex;
characterIndex = [layoutManager characterIndexForPoint:location
inTextContainer:textView.textContainer
fractionOfDistanceBetweenInsertionPoints:NULL];
if (characterIndex < textView.textStorage.length) {
NSRange range;
NSDictionary *attributes = [textView.textStorage attributesAtIndex:characterIndex effectiveRange:&range];
NSLog(@"%@, %@", attributes, NSStringFromRange(range));
//Based on the attributes, do something
///if ([attributes objectForKey:...)] //make a network call, load a cat Pic, etc
}
}
답변
iOS 7에서는 사용자 지정 링크를 만들고 원하는 작업을 수행하는 것이 훨씬 쉬워졌습니다. Ray Wenderlich 에는 아주 좋은 예가 있습니다.
답변
NSLayoutManager *layoutManager = textView.layoutManager;
CGPoint location = [touch locationInView:textView];
NSUInteger characterIndex;
characterIndex = [layoutManager characterIndexForPoint:location
inTextContainer:textView.textContainer
fractionOfDistanceBetweenInsertionPoints:NULL];
if (characterIndex < textView.textStorage.length) {
// valid index
// Find the word range here
// using -enumerateSubstringsInRange:options:usingBlock:
}
답변
NSLinkAttributeName으로 아주 간단하게 해결할 수있었습니다.
스위프트 2
class MyClass: UIViewController, UITextViewDelegate {
@IBOutlet weak var tvBottom: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
let attributedString = NSMutableAttributedString(string: "click me ok?")
attributedString.addAttribute(NSLinkAttributeName, value: "cs://moreinfo", range: NSMakeRange(0, 5))
tvBottom.attributedText = attributedString
tvBottom.delegate = self
}
func textView(textView: UITextView, shouldInteractWithURL URL: NSURL, inRange characterRange: NSRange) -> Bool {
UtilityFunctions.alert("clicked", message: "clicked")
return false
}
}
답변
Swift 3을 사용하여 속성이있는 텍스트에 대한 동작 감지에 대한 완전한 예제
let termsAndConditionsURL = TERMS_CONDITIONS_URL;
let privacyURL = PRIVACY_URL;
override func viewDidLoad() {
super.viewDidLoad()
self.txtView.delegate = self
let str = "By continuing, you accept the Terms of use and Privacy policy"
let attributedString = NSMutableAttributedString(string: str)
var foundRange = attributedString.mutableString.range(of: "Terms of use") //mention the parts of the attributed text you want to tap and get an custom action
attributedString.addAttribute(NSLinkAttributeName, value: termsAndConditionsURL, range: foundRange)
foundRange = attributedString.mutableString.range(of: "Privacy policy")
attributedString.addAttribute(NSLinkAttributeName, value: privacyURL, range: foundRange)
txtView.attributedText = attributedString
}
그런 다음 shouldInteractWith URL
UITextViewDelegate delegate method로 액션을 잡을 수 있으므로 delegate를 올바르게 설정했는지 확인하십시오.
func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange) -> Bool {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let vc = storyboard.instantiateViewController(withIdentifier: "WebView") as! SKWebViewController
if (URL.absoluteString == termsAndConditionsURL) {
vc.strWebURL = TERMS_CONDITIONS_URL
self.navigationController?.pushViewController(vc, animated: true)
} else if (URL.absoluteString == privacyURL) {
vc.strWebURL = PRIVACY_URL
self.navigationController?.pushViewController(vc, animated: true)
}
return false
}
마찬가지로 요구 사항에 따라 모든 작업을 수행 할 수 있습니다.
건배!!