on iOS, when VoiceOver is running, double click the UITextView can quickly switch the cursor between the beginning and end of the text.
Problem
the problem is, when the text contains some NSTextAttachment, this ability has failed.
Here is some code to reproduce this problem.
import UIKit
class ViewController: UIViewController {
private var textView: UITextView?
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
view.backgroundColor = UIColor.white
textView = UITextView(frame: CGRect(x: 60, y: 220, width: 240, height: 180))
textView?.layer.borderColor = UIColor.black.cgColor
textView?.layer.borderWidth = 1
textView?.font = .systemFont(ofSize: 18)
view.addSubview(textView!)
let button = UIButton(type: .system)
button.frame = CGRect(x: 120, y: 120, width: 60, height: 60)
button.setTitle("click", for: .normal)
button.addTarget(self, action: #selector(self.click), for: .touchUpInside)
view.addSubview(button)
}
@objc func click() {
let emojiImage = UIImage(systemName: "iphone.circle")
let lineHeight = UIFont.systemFont(ofSize: textView!.font?.pointSize ?? 0.0).lineHeight
let insertedAttr = generateAttributedString(image: emojiImage, bounds: CGRect(x: 0, y: -4, width: lineHeight, height: lineHeight))
let attr = NSMutableAttributedString(attributedString: textView!.attributedText)
attr.replaceCharacters(in: textView!.selectedRange, with: insertedAttr)
textView!.attributedText = attr
}
public func generateAttributedString(image: UIImage?, bounds: CGRect) -> NSMutableAttributedString {
let attachment = NSTextAttachment(data: nil, ofType: nil)
attachment.bounds = bounds
attachment.image = image
let attrString = NSMutableAttributedString(attributedString: NSAttributedString(attachment: attachment))
attrString.addAttribute(.font, value: textView!.font!, range: NSRange(location: 0, length: attrString.length))
return attrString
}
}
Post
Replies
Boosts
Views
Activity
I want to query the apple ring's data today, but when i write the code like below, it will crash. It seem that only crash in IOS13.
here is the ternimate information:
** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Unexpected dataType of class HKActivitySummaryType for startDate predicate'
terminating with uncaught exception of type NSException
CoreSimulator 776.3 - Device: iPhone 8 (65B9F36C-53AA-41C4-9F07-0FE9D0742897) - Runtime: iOS 13.5 (17F61) - DeviceType: iPhone 8
NSDate *now = [NSDate date];
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *components = [calendar
components:(NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay | NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond)
fromDate:now];
//
[components setHour:0];
[components setMinute:0];
[components setSecond:0];
NSDate *dayStart = [calendar dateFromComponents:components];
//
[components setHour:24];
[components setMinute:0];
[components setSecond:1];
NSDate *dayEnd = [calendar dateFromComponents:components];
NSLog(@"query summary from %@ to %@", dayStart, dayEnd);
NSPredicate *predicateDate = [HKQuery predicateForSamplesWithStartDate:dayStart
endDate:dayEnd
options:HKQueryOptionStrictStartDate | HKQueryOptionStrictEndDate];
HKActivitySummaryQuery *summaryQuery = [[HKActivitySummaryQuery alloc]
initWithPredicate:predicateDate
resultsHandler:^(HKActivitySummaryQuery *_Nonnull query, NSArray<HKActivitySummary *> *_Nullable activitySummaries, NSError *_Nullable error) {
NSLog(@"get query summary count:%lu", (unsigned long)activitySummaries.count);
}];
[self.healthStore executeQuery:summaryQuery];