SwiftとCoreTextと文字組み

 SwiftとCoreTextと文字組み

86b45faad28dfccd89b4ec04ef00c62e?s=128

Taishi Ikai

July 26, 2016
Tweet

Transcript

  1. !.FMPOTXJGU ழࣂେࢤ೔ຊܦࡁ৽ฉࣾ 4XJGUͱ$PSF5FYUͱจࣈ૊Έ

  2. 2 ழࣂେࢤʢ*LBJ5BJTIJʣ ೔ຊܦࡁ৽ฉࣾͷΤϯδχΞ ࢴ໘ϏϡʔΞʔΞϓϦͷ։ൃ 4XJGU/PEFKT1ZUIPO ϝϩϯ೿ ࣗݾ঺հ

  3. จࣈ૊Έ͍ͯ͠·͔͢ʁ 3

  4. 4 ग़యIUUQPFDOFUTZPLV[BJNFMPO ΞʔϧεɾϑΣϘϦοτ ʢผ໊ʣϚεΫϝϩϯ ໢ϝϩϯͷ୅ද඼छͰ͢ɻՌൽ͸྘৭Ͱશ໘ ʹඒ͍͠໢໨໛༷͕Ͱ͍ͯ·͢ɻ

  5. 5 ग़యIUUQPFDOFUTZPLV[BJNFMPO ΞʔϧεɾϑΣϘϦοτ ʢผ໊ʣϚεΫϝϩϯ ໢ϝϩϯͷ୅ද඼छͰ͢ɻՌൽ͸྘৭Ͱશ໘ ʹඒ͍͠໢໨໛༷͕Ͱ͍ͯ·͢ɻ

  6. 6 ग़యIUUQPFDOFUTZPLV[BJNFMPO ΞʔϧεɾϑΣϘϦοτ ʢผ໊ʣϚεΫϝϩϯ ໢ϝϩϯͷ୅ද඼छͰ͢ɻՌൽ͸྘৭Ͱશ໘ ʹඒ͍͠໢໨໛༷͕Ͱ͍ͯ·͢ɻ

  7. 7 ग़యIUUQPFDOFUTZPLV[BJNFMPO ΞʔϧεɾϑΣϘϦοτ ʢผ໊ʣϚεΫϝϩϯ ໢ϝϩϯͷ୅ද඼छͰ͢ɻՌൽ͸྘৭Ͱશ໘ ʹඒ͍͠໢໨໛༷͕Ͱ͍ͯ·͢ɻ ʢผ໊ʣ

  8. ΞʔϧεɾϑΣϘϦοτ ໢ϝϩϯͷ୅ද඼छͰ͢ɻՌൽ͸྘৭Ͱશ໘ ʹඒ͍͠໢໨໛༷͕Ͱ͍ͯ·͢ɻ ʢผ໊ʣϚεΫϝϩϯ 8 ग़యIUUQPFDOFUTZPLV[BJNFMPO ʢผ໊ʣ ʢผ໊ʣ

  9. 9 ग़యIUUQPFDOFUTZPLV[BJNFMPO ΞʔϧεɾϑΣϘϦοτ ໢ϝϩϯͷ୅ද඼छͰ͢ɻՌൽ͸྘৭Ͱશ໘ ʹඒ͍͠໢໨໛༷͕Ͱ͍ͯ·͢ɻ ʢผ໊ʣϚεΫϝϩϯ

  10. 10

  11. ߦ಄ͷׅހΛͭΊΔฤ $PSF5FYUΛ࢖ͬͨඳը 11

  12. 12  /4"UUSJCVUFE4USJOH͔Β$55ZQFTFUUFSΛੜ੒
 
  Ұߦͷ௕͞ %PVCMF Λ͋ͨ͑ͯɺվߦ͢Δ·Ͱͷจࣈ਺ ΛಘΔ var

    typesetter = CTTypesetterCreateWithAttributedString(
 attributedText) let count = CTTypesetterSuggestLineBreak(
 typesetter, 0, Double(lineWidth)) $PSF5FYUΛ࢖ͬͨඳըʕߦ಄ͷׅހΛͭΊΔฤ ໢ϝϩϯͷ୅ද඼छͰ͢ɻՌൽ͸྘৭Ͱશ໘ʹ
 ʹ 20 21 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1
  13. 13  $55ZQFTFUUFS͔ΒҰߦ෼ͷ$5-JOFΛੜ੒
 
  ߦ಄͕࢝ΊׅހͳΒɺӈʹدͤΔ෼ͷ௕͞ΛಘΔ let ctline = CTTypesetterCreateLine(


    typesetter, CFRangeMake(0, count)) let offsetX = fontSize / 2 // ͱΓ͋͑ͣϑΥϯταΠζͷ൒෼ʹ $PSF5FYUΛ࢖ͬͨඳըʕߦ಄ͷׅހΛͭΊΔฤ ʢ
  14. 14  ӈʹدͤΔ෼Λઃఆͯ͠ɺ$5-JOFΛඳը
 
  ߦ਺෼͘Γ͔͑͢ CGContextSetTextPosition(context, offsetX, offsetY)
 CTLineDraw(ctline,

    context) $PSF5FYUΛ࢖ͬͨඳըʕߦ಄ͷׅހΛͭΊΔฤ ʢผ໊ʣϚεΫϝϩϯ ʢผ໊ʣϚεΫϝϩϯ
  15. 15 /4"UUSJCVUFE4USJOHΑΓ΋̏ഒ͘Β͍஗͍ จࣈ͕ଟ͘ͳΔͱɺNT NT Λ௒͑Δ ͜ͱ΋ ಛʹ$55ZQFTFUUFS$SFBUF8JUI"UUSJCVUFE4USJOH͕ॏ͍ ͸͍ͭ͜ͷ࣌ؒ "UUSJCVUFΛଟ͋ͯ͘Δͱ஗͘ͳΔ ύϑΥʔϚϯε

  16. HJUIVCDPNJLBJU,FSO-BCFM 16 ߦ಄Ґஔௐ੔ɺࠨɾதԝɾӈɾۉ౳ἧ͑ɺ
 ྡ઀͢Δ໿෺ͷִؒΛௐ੔ɺ۟ಡ఺ͷ௥͍ग़͠ͳͲ ϥΠϒϥϦΛ͍ͭͬͯ͘·͢

  17. $PSF5FYU͸4XJGUͰͲ͏มԽ 17 ͓·͚

  18. 18 4XJGU 4XJGU $PSF(SBQIJDT

  19. 19 4XJGU 4XJGU $PSF(SBQIJDT

  20. 20 $PSF5FYU 4XJGU 4XJGU