YYTextRubyExample.m 3.6 KB

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