1 // 2 // ViewController.m 3 // Attributor 4 // 5 // Created by qiuda bin on 15/11/26. 6 // Copyright © 2015年 qiuda bin. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 11 @interface ViewController () 12 13 @property (weak, nonatomic) IBOutlet UITextView *body; 14 @property (weak, nonatomic) IBOutlet UILabel *headline; 15 @property (weak, nonatomic) IBOutlet UIButton *outlineButton; 16 17 @end 18 19 @implementation ViewController 20 21 - (void)viewDidLoad { 22 [super viewDidLoad]; 23 // Do any additional setup after loading the view, typically from a nib. 24 NSMutableAttributedString *title = 25 [[NSMutableAttributedString alloc] initWithString:self.outlineButton.currentTitle]; 26 27 [title setAttributes:@{NSStrokeWidthAttributeName: @3, 28 NSStrokeColorAttributeName: self.outlineButton.tintColor} 29 range:NSMakeRange(0, [title length])]; 30 [self.outlineButton setAttributedTitle:title forState:UIControlStateNormal]; 31 } 32 33 - (void)viewWillAppear:(BOOL)animated 34 { 35 [super viewWillDisappear:animated]; 36 [self usePerferredFonts]; 37 [[NSNotificationCenter defaultCenter] addObserver:self 38 selector:@selector(perferredFontsChanged:) 39 name:UIContentSizeCategoryDidChangeNotification 40 object:nil]; 41 } 42 43 - (void)perferredFontsChanged:(NSNotification *)notification 44 { 45 [self usePerferredFonts]; 46 } 47 48 - (void) usePerferredFonts 49 { 50 self.body.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody]; 51 self.headline.font = [UIFont preferredFontForTextStyle:UIFontTextStyleHeadline]; 52 } 53 54 - (void)viewWillDisappear:(BOOL)animated 55 { 56 [super viewWillDisappear:animated]; 57 [[NSNotificationCenter defaultCenter] removeObserver:self 58 name:UIContentSizeCategoryDidChangeNotification 59 object:nil]; 60 61 } 62 63 - (IBAction)changeBodySelectedColorMatchButtonBackgroundColor:(UIButton *)sender { 64 [self.body.textStorage addAttribute:NSForegroundColorAttributeName 65 value:sender.backgroundColor 66 range:self.body.selectedRange]; 67 } 68 69 - (IBAction)outlineBodySelection { 70 [self.body.textStorage addAttributes:@{NSStrokeWidthAttributeName:@-3, 71 NSStrokeColorAttributeName:[UIColor blackColor]} 72 range:self.body.selectedRange]; 73 } 74 75 - (IBAction)unoutlineBodySelection { 76 [self.body.textStorage removeAttribute:NSStrokeWidthAttributeName 77 range:self.body.selectedRange]; 78 } 79 80 81 - (void)didReceiveMemoryWarning { 82 [super didReceiveMemoryWarning]; 83 // Dispose of any resources that can be recreated. 84 } 85 86 @end 87 88
时间: 2024-11-20 16:47:11