YYTextRubyExample.m 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //
  2. // YYTextRubyExample.m
  3. // YYKitExample
  4. //
  5. // Created by ibireme on 15/9/9.
  6. // Copyright (C) 2015 ibireme. All rights reserved.
  7. //
  8. #import "YYTextRubyExample.h"
  9. #import "YYText.h"
  10. #import "YYImage.h"
  11. #import "UIImage+YYWebImage.h"
  12. #import "UIView+YYAdd.h"
  13. #import "NSBundle+YYAdd.h"
  14. #import "NSString+YYAdd.h"
  15. #import "YYTextExampleHelper.h"
  16. /*
  17. Ruby Annotation
  18. See: http://www.w3.org/TR/ruby/
  19. */
  20. @implementation YYTextRubyExample
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. self.view.backgroundColor = [UIColor whiteColor];
  24. if ([self respondsToSelector:@selector(setAutomaticallyAdjustsScrollViewInsets:)]) {
  25. self.automaticallyAdjustsScrollViewInsets = NO;
  26. }
  27. NSMutableAttributedString *text = [NSMutableAttributedString new];
  28. if (kSystemVersion < 8) {
  29. [text yy_appendString:@"Only support iOS8 Later"];
  30. text.yy_font = [UIFont systemFontOfSize:30];
  31. } else {
  32. NSMutableAttributedString *one = [[NSMutableAttributedString alloc] initWithString:@"这是用汉语写的一段文字。"];
  33. one.yy_font = [UIFont boldSystemFontOfSize:30];
  34. YYTextRubyAnnotation *ruby;
  35. ruby = [YYTextRubyAnnotation new];
  36. ruby.textBefore = @"hàn yŭ";
  37. [one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"汉语"]];
  38. ruby = [YYTextRubyAnnotation new];
  39. ruby.textBefore = @"wén";
  40. [one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"文"]];
  41. ruby = [YYTextRubyAnnotation new];
  42. ruby.textBefore = @"zì";
  43. ruby.alignment = kCTRubyAlignmentCenter;
  44. [one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"字"]];
  45. [text appendAttributedString:one];
  46. [text appendAttributedString:[self padding]];
  47. one = [[NSMutableAttributedString alloc] initWithString:@"日本語で書いた作文です。"];
  48. one.yy_font = [UIFont boldSystemFontOfSize:30];
  49. ruby = [YYTextRubyAnnotation new];
  50. ruby.textBefore = @"に";
  51. [one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"日"]];
  52. ruby = [YYTextRubyAnnotation new];
  53. ruby.textBefore = @"ほん";
  54. [one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"本"]];
  55. ruby = [YYTextRubyAnnotation new];
  56. ruby.textBefore = @"ご";
  57. [one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"語"]];
  58. ruby = [YYTextRubyAnnotation new];
  59. ruby.textBefore = @"か";
  60. [one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"書"]];
  61. ruby = [YYTextRubyAnnotation new];
  62. ruby.textBefore = @"さく";
  63. [one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"作"]];
  64. ruby = [YYTextRubyAnnotation new];
  65. ruby.textBefore = @"ぶん";
  66. [one yy_setTextRubyAnnotation:ruby range:[one.string rangeOfString:@"文"]];
  67. [text appendAttributedString:one];
  68. }
  69. YYLabel *label = [YYLabel new];
  70. label.attributedText = text;
  71. label.width = self.view.width - 60;
  72. label.centerX = self.view.width / 2;
  73. label.height = self.view.height - (kiOS7Later ? 64 : 44) - 60;
  74. label.top = (kiOS7Later ? 64 : 0) + 30;
  75. label.textAlignment = NSTextAlignmentCenter;
  76. label.textVerticalAlignment = YYTextVerticalAlignmentCenter;
  77. label.numberOfLines = 0;
  78. label.backgroundColor = [UIColor colorWithWhite:0.933 alpha:1.000];
  79. [self.view addSubview:label];
  80. }
  81. - (NSAttributedString *)padding {
  82. NSMutableAttributedString *pad = [[NSMutableAttributedString alloc] initWithString:@"\n\n"];
  83. pad.yy_font = [UIFont systemFontOfSize:30];
  84. return pad;
  85. }
  86. @end