├── .gitignore ├── AttributedStringStyledMarkdown.xcodeproj ├── project.pbxproj └── project.xcworkspace │ ├── contents.xcworkspacedata │ └── xcshareddata │ └── IDEWorkspaceChecks.plist ├── AttributedStringStyledMarkdown ├── AppDelegate.swift ├── Assets.xcassets │ ├── AccentColor.colorset │ │ └── Contents.json │ ├── AppIcon.appiconset │ │ └── Contents.json │ └── Contents.json ├── AttributedString+StyledMarkdown.swift ├── Base.lproj │ ├── LaunchScreen.storyboard │ └── Main.storyboard ├── DrawView.swift ├── Info.plist ├── SceneDelegate.swift └── ViewController.swift ├── AttributedStringStyledMarkdownTests └── AttributedStringStyledMarkdownTests.swift ├── AttributedStringStyledMarkdownUITests ├── AttributedStringStyledMarkdownUITests.swift └── AttributedStringStyledMarkdownUITestsLaunchTests.swift ├── LICENSE ├── README.md ├── attributedstring-markdown-dark.jpg └── attributedstring-markdown-light.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | # macOS 2 | .DS_Store 3 | 4 | # Xcode 5 | xcuserdata/ 6 | 7 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdown.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 55; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | 276343D82746A3BE00479002 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 276343D72746A3BE00479002 /* AppDelegate.swift */; }; 11 | 276343DA2746A3BE00479002 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 276343D92746A3BE00479002 /* SceneDelegate.swift */; }; 12 | 276343DC2746A3BE00479002 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 276343DB2746A3BE00479002 /* ViewController.swift */; }; 13 | 276343DF2746A3BE00479002 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 276343DD2746A3BE00479002 /* Main.storyboard */; }; 14 | 276343E12746A3BF00479002 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 276343E02746A3BF00479002 /* Assets.xcassets */; }; 15 | 276343E42746A3BF00479002 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 276343E22746A3BF00479002 /* LaunchScreen.storyboard */; }; 16 | 276343EF2746A3BF00479002 /* AttributedStringStyledMarkdownTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 276343EE2746A3BF00479002 /* AttributedStringStyledMarkdownTests.swift */; }; 17 | 276343F92746A3BF00479002 /* AttributedStringStyledMarkdownUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 276343F82746A3BF00479002 /* AttributedStringStyledMarkdownUITests.swift */; }; 18 | 276343FB2746A3BF00479002 /* AttributedStringStyledMarkdownUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 276343FA2746A3BF00479002 /* AttributedStringStyledMarkdownUITestsLaunchTests.swift */; }; 19 | 276344082746A41E00479002 /* AttributedString+StyledMarkdown.swift in Sources */ = {isa = PBXBuildFile; fileRef = 276344072746A41E00479002 /* AttributedString+StyledMarkdown.swift */; }; 20 | 2763440A2746A44200479002 /* DrawView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 276344092746A44200479002 /* DrawView.swift */; }; 21 | /* End PBXBuildFile section */ 22 | 23 | /* Begin PBXContainerItemProxy section */ 24 | 276343EB2746A3BF00479002 /* PBXContainerItemProxy */ = { 25 | isa = PBXContainerItemProxy; 26 | containerPortal = 276343CC2746A3BE00479002 /* Project object */; 27 | proxyType = 1; 28 | remoteGlobalIDString = 276343D32746A3BE00479002; 29 | remoteInfo = AttributedStringStyledMarkdown; 30 | }; 31 | 276343F52746A3BF00479002 /* PBXContainerItemProxy */ = { 32 | isa = PBXContainerItemProxy; 33 | containerPortal = 276343CC2746A3BE00479002 /* Project object */; 34 | proxyType = 1; 35 | remoteGlobalIDString = 276343D32746A3BE00479002; 36 | remoteInfo = AttributedStringStyledMarkdown; 37 | }; 38 | /* End PBXContainerItemProxy section */ 39 | 40 | /* Begin PBXFileReference section */ 41 | 276343D42746A3BE00479002 /* AttributedStringStyledMarkdown.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AttributedStringStyledMarkdown.app; sourceTree = BUILT_PRODUCTS_DIR; }; 42 | 276343D72746A3BE00479002 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 43 | 276343D92746A3BE00479002 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = ""; }; 44 | 276343DB2746A3BE00479002 /* ViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewController.swift; sourceTree = ""; }; 45 | 276343DE2746A3BE00479002 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 46 | 276343E02746A3BF00479002 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 47 | 276343E32746A3BF00479002 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 48 | 276343E52746A3BF00479002 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 49 | 276343EA2746A3BF00479002 /* AttributedStringStyledMarkdownTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AttributedStringStyledMarkdownTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 50 | 276343EE2746A3BF00479002 /* AttributedStringStyledMarkdownTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributedStringStyledMarkdownTests.swift; sourceTree = ""; }; 51 | 276343F42746A3BF00479002 /* AttributedStringStyledMarkdownUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = AttributedStringStyledMarkdownUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 52 | 276343F82746A3BF00479002 /* AttributedStringStyledMarkdownUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributedStringStyledMarkdownUITests.swift; sourceTree = ""; }; 53 | 276343FA2746A3BF00479002 /* AttributedStringStyledMarkdownUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttributedStringStyledMarkdownUITestsLaunchTests.swift; sourceTree = ""; }; 54 | 276344072746A41E00479002 /* AttributedString+StyledMarkdown.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "AttributedString+StyledMarkdown.swift"; sourceTree = ""; }; 55 | 276344092746A44200479002 /* DrawView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DrawView.swift; sourceTree = ""; }; 56 | /* End PBXFileReference section */ 57 | 58 | /* Begin PBXFrameworksBuildPhase section */ 59 | 276343D12746A3BE00479002 /* Frameworks */ = { 60 | isa = PBXFrameworksBuildPhase; 61 | buildActionMask = 2147483647; 62 | files = ( 63 | ); 64 | runOnlyForDeploymentPostprocessing = 0; 65 | }; 66 | 276343E72746A3BF00479002 /* Frameworks */ = { 67 | isa = PBXFrameworksBuildPhase; 68 | buildActionMask = 2147483647; 69 | files = ( 70 | ); 71 | runOnlyForDeploymentPostprocessing = 0; 72 | }; 73 | 276343F12746A3BF00479002 /* Frameworks */ = { 74 | isa = PBXFrameworksBuildPhase; 75 | buildActionMask = 2147483647; 76 | files = ( 77 | ); 78 | runOnlyForDeploymentPostprocessing = 0; 79 | }; 80 | /* End PBXFrameworksBuildPhase section */ 81 | 82 | /* Begin PBXGroup section */ 83 | 276343CB2746A3BE00479002 = { 84 | isa = PBXGroup; 85 | children = ( 86 | 276343D62746A3BE00479002 /* AttributedStringStyledMarkdown */, 87 | 276343ED2746A3BF00479002 /* AttributedStringStyledMarkdownTests */, 88 | 276343F72746A3BF00479002 /* AttributedStringStyledMarkdownUITests */, 89 | 276343D52746A3BE00479002 /* Products */, 90 | ); 91 | sourceTree = ""; 92 | }; 93 | 276343D52746A3BE00479002 /* Products */ = { 94 | isa = PBXGroup; 95 | children = ( 96 | 276343D42746A3BE00479002 /* AttributedStringStyledMarkdown.app */, 97 | 276343EA2746A3BF00479002 /* AttributedStringStyledMarkdownTests.xctest */, 98 | 276343F42746A3BF00479002 /* AttributedStringStyledMarkdownUITests.xctest */, 99 | ); 100 | name = Products; 101 | sourceTree = ""; 102 | }; 103 | 276343D62746A3BE00479002 /* AttributedStringStyledMarkdown */ = { 104 | isa = PBXGroup; 105 | children = ( 106 | 276343D72746A3BE00479002 /* AppDelegate.swift */, 107 | 276343D92746A3BE00479002 /* SceneDelegate.swift */, 108 | 276343DB2746A3BE00479002 /* ViewController.swift */, 109 | 276344072746A41E00479002 /* AttributedString+StyledMarkdown.swift */, 110 | 276344092746A44200479002 /* DrawView.swift */, 111 | 276343DD2746A3BE00479002 /* Main.storyboard */, 112 | 276343E02746A3BF00479002 /* Assets.xcassets */, 113 | 276343E22746A3BF00479002 /* LaunchScreen.storyboard */, 114 | 276343E52746A3BF00479002 /* Info.plist */, 115 | ); 116 | path = AttributedStringStyledMarkdown; 117 | sourceTree = ""; 118 | }; 119 | 276343ED2746A3BF00479002 /* AttributedStringStyledMarkdownTests */ = { 120 | isa = PBXGroup; 121 | children = ( 122 | 276343EE2746A3BF00479002 /* AttributedStringStyledMarkdownTests.swift */, 123 | ); 124 | path = AttributedStringStyledMarkdownTests; 125 | sourceTree = ""; 126 | }; 127 | 276343F72746A3BF00479002 /* AttributedStringStyledMarkdownUITests */ = { 128 | isa = PBXGroup; 129 | children = ( 130 | 276343F82746A3BF00479002 /* AttributedStringStyledMarkdownUITests.swift */, 131 | 276343FA2746A3BF00479002 /* AttributedStringStyledMarkdownUITestsLaunchTests.swift */, 132 | ); 133 | path = AttributedStringStyledMarkdownUITests; 134 | sourceTree = ""; 135 | }; 136 | /* End PBXGroup section */ 137 | 138 | /* Begin PBXNativeTarget section */ 139 | 276343D32746A3BE00479002 /* AttributedStringStyledMarkdown */ = { 140 | isa = PBXNativeTarget; 141 | buildConfigurationList = 276343FE2746A3BF00479002 /* Build configuration list for PBXNativeTarget "AttributedStringStyledMarkdown" */; 142 | buildPhases = ( 143 | 276343D02746A3BE00479002 /* Sources */, 144 | 276343D12746A3BE00479002 /* Frameworks */, 145 | 276343D22746A3BE00479002 /* Resources */, 146 | ); 147 | buildRules = ( 148 | ); 149 | dependencies = ( 150 | ); 151 | name = AttributedStringStyledMarkdown; 152 | productName = AttributedStringStyledMarkdown; 153 | productReference = 276343D42746A3BE00479002 /* AttributedStringStyledMarkdown.app */; 154 | productType = "com.apple.product-type.application"; 155 | }; 156 | 276343E92746A3BF00479002 /* AttributedStringStyledMarkdownTests */ = { 157 | isa = PBXNativeTarget; 158 | buildConfigurationList = 276344012746A3BF00479002 /* Build configuration list for PBXNativeTarget "AttributedStringStyledMarkdownTests" */; 159 | buildPhases = ( 160 | 276343E62746A3BF00479002 /* Sources */, 161 | 276343E72746A3BF00479002 /* Frameworks */, 162 | 276343E82746A3BF00479002 /* Resources */, 163 | ); 164 | buildRules = ( 165 | ); 166 | dependencies = ( 167 | 276343EC2746A3BF00479002 /* PBXTargetDependency */, 168 | ); 169 | name = AttributedStringStyledMarkdownTests; 170 | productName = AttributedStringStyledMarkdownTests; 171 | productReference = 276343EA2746A3BF00479002 /* AttributedStringStyledMarkdownTests.xctest */; 172 | productType = "com.apple.product-type.bundle.unit-test"; 173 | }; 174 | 276343F32746A3BF00479002 /* AttributedStringStyledMarkdownUITests */ = { 175 | isa = PBXNativeTarget; 176 | buildConfigurationList = 276344042746A3BF00479002 /* Build configuration list for PBXNativeTarget "AttributedStringStyledMarkdownUITests" */; 177 | buildPhases = ( 178 | 276343F02746A3BF00479002 /* Sources */, 179 | 276343F12746A3BF00479002 /* Frameworks */, 180 | 276343F22746A3BF00479002 /* Resources */, 181 | ); 182 | buildRules = ( 183 | ); 184 | dependencies = ( 185 | 276343F62746A3BF00479002 /* PBXTargetDependency */, 186 | ); 187 | name = AttributedStringStyledMarkdownUITests; 188 | productName = AttributedStringStyledMarkdownUITests; 189 | productReference = 276343F42746A3BF00479002 /* AttributedStringStyledMarkdownUITests.xctest */; 190 | productType = "com.apple.product-type.bundle.ui-testing"; 191 | }; 192 | /* End PBXNativeTarget section */ 193 | 194 | /* Begin PBXProject section */ 195 | 276343CC2746A3BE00479002 /* Project object */ = { 196 | isa = PBXProject; 197 | attributes = { 198 | BuildIndependentTargetsInParallel = 1; 199 | LastSwiftUpdateCheck = 1310; 200 | LastUpgradeCheck = 1310; 201 | TargetAttributes = { 202 | 276343D32746A3BE00479002 = { 203 | CreatedOnToolsVersion = 13.1; 204 | }; 205 | 276343E92746A3BF00479002 = { 206 | CreatedOnToolsVersion = 13.1; 207 | TestTargetID = 276343D32746A3BE00479002; 208 | }; 209 | 276343F32746A3BF00479002 = { 210 | CreatedOnToolsVersion = 13.1; 211 | TestTargetID = 276343D32746A3BE00479002; 212 | }; 213 | }; 214 | }; 215 | buildConfigurationList = 276343CF2746A3BE00479002 /* Build configuration list for PBXProject "AttributedStringStyledMarkdown" */; 216 | compatibilityVersion = "Xcode 13.0"; 217 | developmentRegion = en; 218 | hasScannedForEncodings = 0; 219 | knownRegions = ( 220 | en, 221 | Base, 222 | ); 223 | mainGroup = 276343CB2746A3BE00479002; 224 | productRefGroup = 276343D52746A3BE00479002 /* Products */; 225 | projectDirPath = ""; 226 | projectRoot = ""; 227 | targets = ( 228 | 276343D32746A3BE00479002 /* AttributedStringStyledMarkdown */, 229 | 276343E92746A3BF00479002 /* AttributedStringStyledMarkdownTests */, 230 | 276343F32746A3BF00479002 /* AttributedStringStyledMarkdownUITests */, 231 | ); 232 | }; 233 | /* End PBXProject section */ 234 | 235 | /* Begin PBXResourcesBuildPhase section */ 236 | 276343D22746A3BE00479002 /* Resources */ = { 237 | isa = PBXResourcesBuildPhase; 238 | buildActionMask = 2147483647; 239 | files = ( 240 | 276343E42746A3BF00479002 /* LaunchScreen.storyboard in Resources */, 241 | 276343E12746A3BF00479002 /* Assets.xcassets in Resources */, 242 | 276343DF2746A3BE00479002 /* Main.storyboard in Resources */, 243 | ); 244 | runOnlyForDeploymentPostprocessing = 0; 245 | }; 246 | 276343E82746A3BF00479002 /* Resources */ = { 247 | isa = PBXResourcesBuildPhase; 248 | buildActionMask = 2147483647; 249 | files = ( 250 | ); 251 | runOnlyForDeploymentPostprocessing = 0; 252 | }; 253 | 276343F22746A3BF00479002 /* Resources */ = { 254 | isa = PBXResourcesBuildPhase; 255 | buildActionMask = 2147483647; 256 | files = ( 257 | ); 258 | runOnlyForDeploymentPostprocessing = 0; 259 | }; 260 | /* End PBXResourcesBuildPhase section */ 261 | 262 | /* Begin PBXSourcesBuildPhase section */ 263 | 276343D02746A3BE00479002 /* Sources */ = { 264 | isa = PBXSourcesBuildPhase; 265 | buildActionMask = 2147483647; 266 | files = ( 267 | 276343DC2746A3BE00479002 /* ViewController.swift in Sources */, 268 | 2763440A2746A44200479002 /* DrawView.swift in Sources */, 269 | 276343D82746A3BE00479002 /* AppDelegate.swift in Sources */, 270 | 276344082746A41E00479002 /* AttributedString+StyledMarkdown.swift in Sources */, 271 | 276343DA2746A3BE00479002 /* SceneDelegate.swift in Sources */, 272 | ); 273 | runOnlyForDeploymentPostprocessing = 0; 274 | }; 275 | 276343E62746A3BF00479002 /* Sources */ = { 276 | isa = PBXSourcesBuildPhase; 277 | buildActionMask = 2147483647; 278 | files = ( 279 | 276343EF2746A3BF00479002 /* AttributedStringStyledMarkdownTests.swift in Sources */, 280 | ); 281 | runOnlyForDeploymentPostprocessing = 0; 282 | }; 283 | 276343F02746A3BF00479002 /* Sources */ = { 284 | isa = PBXSourcesBuildPhase; 285 | buildActionMask = 2147483647; 286 | files = ( 287 | 276343F92746A3BF00479002 /* AttributedStringStyledMarkdownUITests.swift in Sources */, 288 | 276343FB2746A3BF00479002 /* AttributedStringStyledMarkdownUITestsLaunchTests.swift in Sources */, 289 | ); 290 | runOnlyForDeploymentPostprocessing = 0; 291 | }; 292 | /* End PBXSourcesBuildPhase section */ 293 | 294 | /* Begin PBXTargetDependency section */ 295 | 276343EC2746A3BF00479002 /* PBXTargetDependency */ = { 296 | isa = PBXTargetDependency; 297 | target = 276343D32746A3BE00479002 /* AttributedStringStyledMarkdown */; 298 | targetProxy = 276343EB2746A3BF00479002 /* PBXContainerItemProxy */; 299 | }; 300 | 276343F62746A3BF00479002 /* PBXTargetDependency */ = { 301 | isa = PBXTargetDependency; 302 | target = 276343D32746A3BE00479002 /* AttributedStringStyledMarkdown */; 303 | targetProxy = 276343F52746A3BF00479002 /* PBXContainerItemProxy */; 304 | }; 305 | /* End PBXTargetDependency section */ 306 | 307 | /* Begin PBXVariantGroup section */ 308 | 276343DD2746A3BE00479002 /* Main.storyboard */ = { 309 | isa = PBXVariantGroup; 310 | children = ( 311 | 276343DE2746A3BE00479002 /* Base */, 312 | ); 313 | name = Main.storyboard; 314 | sourceTree = ""; 315 | }; 316 | 276343E22746A3BF00479002 /* LaunchScreen.storyboard */ = { 317 | isa = PBXVariantGroup; 318 | children = ( 319 | 276343E32746A3BF00479002 /* Base */, 320 | ); 321 | name = LaunchScreen.storyboard; 322 | sourceTree = ""; 323 | }; 324 | /* End PBXVariantGroup section */ 325 | 326 | /* Begin XCBuildConfiguration section */ 327 | 276343FC2746A3BF00479002 /* Debug */ = { 328 | isa = XCBuildConfiguration; 329 | buildSettings = { 330 | ALWAYS_SEARCH_USER_PATHS = NO; 331 | CLANG_ANALYZER_NONNULL = YES; 332 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 333 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 334 | CLANG_CXX_LIBRARY = "libc++"; 335 | CLANG_ENABLE_MODULES = YES; 336 | CLANG_ENABLE_OBJC_ARC = YES; 337 | CLANG_ENABLE_OBJC_WEAK = YES; 338 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 339 | CLANG_WARN_BOOL_CONVERSION = YES; 340 | CLANG_WARN_COMMA = YES; 341 | CLANG_WARN_CONSTANT_CONVERSION = YES; 342 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 343 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 344 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 345 | CLANG_WARN_EMPTY_BODY = YES; 346 | CLANG_WARN_ENUM_CONVERSION = YES; 347 | CLANG_WARN_INFINITE_RECURSION = YES; 348 | CLANG_WARN_INT_CONVERSION = YES; 349 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 350 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 351 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 352 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 353 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 354 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 355 | CLANG_WARN_STRICT_PROTOTYPES = YES; 356 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 357 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 358 | CLANG_WARN_UNREACHABLE_CODE = YES; 359 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 360 | COPY_PHASE_STRIP = NO; 361 | DEBUG_INFORMATION_FORMAT = dwarf; 362 | ENABLE_STRICT_OBJC_MSGSEND = YES; 363 | ENABLE_TESTABILITY = YES; 364 | GCC_C_LANGUAGE_STANDARD = gnu11; 365 | GCC_DYNAMIC_NO_PIC = NO; 366 | GCC_NO_COMMON_BLOCKS = YES; 367 | GCC_OPTIMIZATION_LEVEL = 0; 368 | GCC_PREPROCESSOR_DEFINITIONS = ( 369 | "DEBUG=1", 370 | "$(inherited)", 371 | ); 372 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 373 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 374 | GCC_WARN_UNDECLARED_SELECTOR = YES; 375 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 376 | GCC_WARN_UNUSED_FUNCTION = YES; 377 | GCC_WARN_UNUSED_VARIABLE = YES; 378 | IPHONEOS_DEPLOYMENT_TARGET = 15.0; 379 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 380 | MTL_FAST_MATH = YES; 381 | ONLY_ACTIVE_ARCH = YES; 382 | SDKROOT = iphoneos; 383 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 384 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 385 | }; 386 | name = Debug; 387 | }; 388 | 276343FD2746A3BF00479002 /* Release */ = { 389 | isa = XCBuildConfiguration; 390 | buildSettings = { 391 | ALWAYS_SEARCH_USER_PATHS = NO; 392 | CLANG_ANALYZER_NONNULL = YES; 393 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 394 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++17"; 395 | CLANG_CXX_LIBRARY = "libc++"; 396 | CLANG_ENABLE_MODULES = YES; 397 | CLANG_ENABLE_OBJC_ARC = YES; 398 | CLANG_ENABLE_OBJC_WEAK = YES; 399 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 400 | CLANG_WARN_BOOL_CONVERSION = YES; 401 | CLANG_WARN_COMMA = YES; 402 | CLANG_WARN_CONSTANT_CONVERSION = YES; 403 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 404 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 405 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 406 | CLANG_WARN_EMPTY_BODY = YES; 407 | CLANG_WARN_ENUM_CONVERSION = YES; 408 | CLANG_WARN_INFINITE_RECURSION = YES; 409 | CLANG_WARN_INT_CONVERSION = YES; 410 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 411 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 412 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 413 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 414 | CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; 415 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 416 | CLANG_WARN_STRICT_PROTOTYPES = YES; 417 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 418 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 419 | CLANG_WARN_UNREACHABLE_CODE = YES; 420 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 421 | COPY_PHASE_STRIP = NO; 422 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 423 | ENABLE_NS_ASSERTIONS = NO; 424 | ENABLE_STRICT_OBJC_MSGSEND = YES; 425 | GCC_C_LANGUAGE_STANDARD = gnu11; 426 | GCC_NO_COMMON_BLOCKS = YES; 427 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 428 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 429 | GCC_WARN_UNDECLARED_SELECTOR = YES; 430 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 431 | GCC_WARN_UNUSED_FUNCTION = YES; 432 | GCC_WARN_UNUSED_VARIABLE = YES; 433 | IPHONEOS_DEPLOYMENT_TARGET = 15.0; 434 | MTL_ENABLE_DEBUG_INFO = NO; 435 | MTL_FAST_MATH = YES; 436 | SDKROOT = iphoneos; 437 | SWIFT_COMPILATION_MODE = wholemodule; 438 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 439 | VALIDATE_PRODUCT = YES; 440 | }; 441 | name = Release; 442 | }; 443 | 276343FF2746A3BF00479002 /* Debug */ = { 444 | isa = XCBuildConfiguration; 445 | buildSettings = { 446 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 447 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 448 | CODE_SIGN_STYLE = Automatic; 449 | CURRENT_PROJECT_VERSION = 1; 450 | DEVELOPMENT_TEAM = ""; 451 | GENERATE_INFOPLIST_FILE = YES; 452 | INFOPLIST_FILE = AttributedStringStyledMarkdown/Info.plist; 453 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 454 | INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; 455 | INFOPLIST_KEY_UIMainStoryboardFile = Main; 456 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 457 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 458 | LD_RUNPATH_SEARCH_PATHS = ( 459 | "$(inherited)", 460 | "@executable_path/Frameworks", 461 | ); 462 | MARKETING_VERSION = 1.0; 463 | PRODUCT_BUNDLE_IDENTIFIER = net.raureif.AttributedStringStyledMarkdown; 464 | PRODUCT_NAME = "$(TARGET_NAME)"; 465 | SWIFT_EMIT_LOC_STRINGS = YES; 466 | SWIFT_VERSION = 5.0; 467 | TARGETED_DEVICE_FAMILY = "1,2"; 468 | }; 469 | name = Debug; 470 | }; 471 | 276344002746A3BF00479002 /* Release */ = { 472 | isa = XCBuildConfiguration; 473 | buildSettings = { 474 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 475 | ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor; 476 | CODE_SIGN_STYLE = Automatic; 477 | CURRENT_PROJECT_VERSION = 1; 478 | DEVELOPMENT_TEAM = ""; 479 | GENERATE_INFOPLIST_FILE = YES; 480 | INFOPLIST_FILE = AttributedStringStyledMarkdown/Info.plist; 481 | INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES; 482 | INFOPLIST_KEY_UILaunchStoryboardName = LaunchScreen; 483 | INFOPLIST_KEY_UIMainStoryboardFile = Main; 484 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad = "UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 485 | INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone = "UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight"; 486 | LD_RUNPATH_SEARCH_PATHS = ( 487 | "$(inherited)", 488 | "@executable_path/Frameworks", 489 | ); 490 | MARKETING_VERSION = 1.0; 491 | PRODUCT_BUNDLE_IDENTIFIER = net.raureif.AttributedStringStyledMarkdown; 492 | PRODUCT_NAME = "$(TARGET_NAME)"; 493 | SWIFT_EMIT_LOC_STRINGS = YES; 494 | SWIFT_VERSION = 5.0; 495 | TARGETED_DEVICE_FAMILY = "1,2"; 496 | }; 497 | name = Release; 498 | }; 499 | 276344022746A3BF00479002 /* Debug */ = { 500 | isa = XCBuildConfiguration; 501 | buildSettings = { 502 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 503 | BUNDLE_LOADER = "$(TEST_HOST)"; 504 | CODE_SIGN_STYLE = Automatic; 505 | CURRENT_PROJECT_VERSION = 1; 506 | DEVELOPMENT_TEAM = 9KYFX28EJA; 507 | GENERATE_INFOPLIST_FILE = YES; 508 | IPHONEOS_DEPLOYMENT_TARGET = 15.0; 509 | LD_RUNPATH_SEARCH_PATHS = ( 510 | "$(inherited)", 511 | "@executable_path/Frameworks", 512 | "@loader_path/Frameworks", 513 | ); 514 | MARKETING_VERSION = 1.0; 515 | PRODUCT_BUNDLE_IDENTIFIER = net.raureif.AttributedStringStyledMarkdownTests; 516 | PRODUCT_NAME = "$(TARGET_NAME)"; 517 | SWIFT_EMIT_LOC_STRINGS = NO; 518 | SWIFT_VERSION = 5.0; 519 | TARGETED_DEVICE_FAMILY = "1,2"; 520 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/AttributedStringStyledMarkdown.app/AttributedStringStyledMarkdown"; 521 | }; 522 | name = Debug; 523 | }; 524 | 276344032746A3BF00479002 /* Release */ = { 525 | isa = XCBuildConfiguration; 526 | buildSettings = { 527 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 528 | BUNDLE_LOADER = "$(TEST_HOST)"; 529 | CODE_SIGN_STYLE = Automatic; 530 | CURRENT_PROJECT_VERSION = 1; 531 | DEVELOPMENT_TEAM = 9KYFX28EJA; 532 | GENERATE_INFOPLIST_FILE = YES; 533 | IPHONEOS_DEPLOYMENT_TARGET = 15.0; 534 | LD_RUNPATH_SEARCH_PATHS = ( 535 | "$(inherited)", 536 | "@executable_path/Frameworks", 537 | "@loader_path/Frameworks", 538 | ); 539 | MARKETING_VERSION = 1.0; 540 | PRODUCT_BUNDLE_IDENTIFIER = net.raureif.AttributedStringStyledMarkdownTests; 541 | PRODUCT_NAME = "$(TARGET_NAME)"; 542 | SWIFT_EMIT_LOC_STRINGS = NO; 543 | SWIFT_VERSION = 5.0; 544 | TARGETED_DEVICE_FAMILY = "1,2"; 545 | TEST_HOST = "$(BUILT_PRODUCTS_DIR)/AttributedStringStyledMarkdown.app/AttributedStringStyledMarkdown"; 546 | }; 547 | name = Release; 548 | }; 549 | 276344052746A3BF00479002 /* Debug */ = { 550 | isa = XCBuildConfiguration; 551 | buildSettings = { 552 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 553 | CODE_SIGN_STYLE = Automatic; 554 | CURRENT_PROJECT_VERSION = 1; 555 | DEVELOPMENT_TEAM = 9KYFX28EJA; 556 | GENERATE_INFOPLIST_FILE = YES; 557 | LD_RUNPATH_SEARCH_PATHS = ( 558 | "$(inherited)", 559 | "@executable_path/Frameworks", 560 | "@loader_path/Frameworks", 561 | ); 562 | MARKETING_VERSION = 1.0; 563 | PRODUCT_BUNDLE_IDENTIFIER = net.raureif.AttributedStringStyledMarkdownUITests; 564 | PRODUCT_NAME = "$(TARGET_NAME)"; 565 | SWIFT_EMIT_LOC_STRINGS = NO; 566 | SWIFT_VERSION = 5.0; 567 | TARGETED_DEVICE_FAMILY = "1,2"; 568 | TEST_TARGET_NAME = AttributedStringStyledMarkdown; 569 | }; 570 | name = Debug; 571 | }; 572 | 276344062746A3BF00479002 /* Release */ = { 573 | isa = XCBuildConfiguration; 574 | buildSettings = { 575 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 576 | CODE_SIGN_STYLE = Automatic; 577 | CURRENT_PROJECT_VERSION = 1; 578 | DEVELOPMENT_TEAM = 9KYFX28EJA; 579 | GENERATE_INFOPLIST_FILE = YES; 580 | LD_RUNPATH_SEARCH_PATHS = ( 581 | "$(inherited)", 582 | "@executable_path/Frameworks", 583 | "@loader_path/Frameworks", 584 | ); 585 | MARKETING_VERSION = 1.0; 586 | PRODUCT_BUNDLE_IDENTIFIER = net.raureif.AttributedStringStyledMarkdownUITests; 587 | PRODUCT_NAME = "$(TARGET_NAME)"; 588 | SWIFT_EMIT_LOC_STRINGS = NO; 589 | SWIFT_VERSION = 5.0; 590 | TARGETED_DEVICE_FAMILY = "1,2"; 591 | TEST_TARGET_NAME = AttributedStringStyledMarkdown; 592 | }; 593 | name = Release; 594 | }; 595 | /* End XCBuildConfiguration section */ 596 | 597 | /* Begin XCConfigurationList section */ 598 | 276343CF2746A3BE00479002 /* Build configuration list for PBXProject "AttributedStringStyledMarkdown" */ = { 599 | isa = XCConfigurationList; 600 | buildConfigurations = ( 601 | 276343FC2746A3BF00479002 /* Debug */, 602 | 276343FD2746A3BF00479002 /* Release */, 603 | ); 604 | defaultConfigurationIsVisible = 0; 605 | defaultConfigurationName = Release; 606 | }; 607 | 276343FE2746A3BF00479002 /* Build configuration list for PBXNativeTarget "AttributedStringStyledMarkdown" */ = { 608 | isa = XCConfigurationList; 609 | buildConfigurations = ( 610 | 276343FF2746A3BF00479002 /* Debug */, 611 | 276344002746A3BF00479002 /* Release */, 612 | ); 613 | defaultConfigurationIsVisible = 0; 614 | defaultConfigurationName = Release; 615 | }; 616 | 276344012746A3BF00479002 /* Build configuration list for PBXNativeTarget "AttributedStringStyledMarkdownTests" */ = { 617 | isa = XCConfigurationList; 618 | buildConfigurations = ( 619 | 276344022746A3BF00479002 /* Debug */, 620 | 276344032746A3BF00479002 /* Release */, 621 | ); 622 | defaultConfigurationIsVisible = 0; 623 | defaultConfigurationName = Release; 624 | }; 625 | 276344042746A3BF00479002 /* Build configuration list for PBXNativeTarget "AttributedStringStyledMarkdownUITests" */ = { 626 | isa = XCConfigurationList; 627 | buildConfigurations = ( 628 | 276344052746A3BF00479002 /* Debug */, 629 | 276344062746A3BF00479002 /* Release */, 630 | ); 631 | defaultConfigurationIsVisible = 0; 632 | defaultConfigurationName = Release; 633 | }; 634 | /* End XCConfigurationList section */ 635 | }; 636 | rootObject = 276343CC2746A3BE00479002 /* Project object */; 637 | } 638 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdown.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdown.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | IDEDidComputeMac32BitWarning 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdown/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Frank Rausch on 2021-11-13. 3 | // 4 | 5 | 6 | import UIKit 7 | 8 | @main 9 | class AppDelegate: UIResponder, UIApplicationDelegate { 10 | 11 | 12 | 13 | func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { 14 | // Override point for customization after application launch. 15 | return true 16 | } 17 | 18 | // MARK: UISceneSession Lifecycle 19 | 20 | func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration { 21 | // Called when a new scene session is being created. 22 | // Use this method to select a configuration to create the new scene with. 23 | return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role) 24 | } 25 | 26 | func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set) { 27 | // Called when the user discards a scene session. 28 | // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions. 29 | // Use this method to release any resources that were specific to the discarded scenes, as they will not return. 30 | } 31 | 32 | 33 | } 34 | 35 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdown/Assets.xcassets/AccentColor.colorset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "colors" : [ 3 | { 4 | "idiom" : "universal" 5 | } 6 | ], 7 | "info" : { 8 | "author" : "xcode", 9 | "version" : 1 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdown/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "scale" : "2x", 6 | "size" : "20x20" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "scale" : "3x", 11 | "size" : "20x20" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "scale" : "2x", 16 | "size" : "29x29" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "scale" : "3x", 21 | "size" : "29x29" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "scale" : "2x", 26 | "size" : "40x40" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "scale" : "3x", 31 | "size" : "40x40" 32 | }, 33 | { 34 | "idiom" : "iphone", 35 | "scale" : "2x", 36 | "size" : "60x60" 37 | }, 38 | { 39 | "idiom" : "iphone", 40 | "scale" : "3x", 41 | "size" : "60x60" 42 | }, 43 | { 44 | "idiom" : "ipad", 45 | "scale" : "1x", 46 | "size" : "20x20" 47 | }, 48 | { 49 | "idiom" : "ipad", 50 | "scale" : "2x", 51 | "size" : "20x20" 52 | }, 53 | { 54 | "idiom" : "ipad", 55 | "scale" : "1x", 56 | "size" : "29x29" 57 | }, 58 | { 59 | "idiom" : "ipad", 60 | "scale" : "2x", 61 | "size" : "29x29" 62 | }, 63 | { 64 | "idiom" : "ipad", 65 | "scale" : "1x", 66 | "size" : "40x40" 67 | }, 68 | { 69 | "idiom" : "ipad", 70 | "scale" : "2x", 71 | "size" : "40x40" 72 | }, 73 | { 74 | "idiom" : "ipad", 75 | "scale" : "1x", 76 | "size" : "76x76" 77 | }, 78 | { 79 | "idiom" : "ipad", 80 | "scale" : "2x", 81 | "size" : "76x76" 82 | }, 83 | { 84 | "idiom" : "ipad", 85 | "scale" : "2x", 86 | "size" : "83.5x83.5" 87 | }, 88 | { 89 | "idiom" : "ios-marketing", 90 | "scale" : "1x", 91 | "size" : "1024x1024" 92 | } 93 | ], 94 | "info" : { 95 | "author" : "xcode", 96 | "version" : 1 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdown/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "author" : "xcode", 4 | "version" : 1 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdown/AttributedString+StyledMarkdown.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Frank Rausch on 2021-11-15. 3 | // 4 | 5 | import UIKit 6 | 7 | fileprivate enum MarkdownStyledBlock: Equatable { 8 | case generic 9 | case headline(Int) 10 | case paragraph 11 | case unorderedListElement 12 | case orderedListElement(Int) 13 | case blockquote 14 | case code(String?) 15 | } 16 | 17 | // MARK: - 18 | 19 | extension AttributedString { 20 | 21 | init(styledMarkdown markdownString: String, fontSize: CGFloat = UIFont.preferredFont(forTextStyle: .body).pointSize) throws { 22 | 23 | var s = try AttributedString(markdown: markdownString, options: .init(allowsExtendedAttributes: true, interpretedSyntax: .full, failurePolicy: .returnPartiallyParsedIfPossible, languageCode: "en"), baseURL: nil) 24 | 25 | // Looking at the AttributedString’s raw structure helps with understanding the following code. 26 | print(s) 27 | 28 | // Set base font and paragraph style for the whole string 29 | s.font = .systemFont(ofSize: fontSize) 30 | s.paragraphStyle = defaultParagraphStyle 31 | 32 | // Will respect dark mode automatically 33 | s.foregroundColor = .label 34 | 35 | // MARK: Inline Intents 36 | let inlineIntents: [InlinePresentationIntent] = [.emphasized, .stronglyEmphasized, .code, .strikethrough, .softBreak, .lineBreak, .inlineHTML, .blockHTML] 37 | 38 | for inlineIntent in inlineIntents { 39 | 40 | var sourceAttributeContainer = AttributeContainer() 41 | sourceAttributeContainer.inlinePresentationIntent = inlineIntent 42 | 43 | var targetAttributeContainer = AttributeContainer() 44 | switch inlineIntent { 45 | case .emphasized: 46 | targetAttributeContainer.font = .italicSystemFont(ofSize: fontSize) 47 | case .stronglyEmphasized: 48 | targetAttributeContainer.font = .systemFont(ofSize: fontSize, weight: .bold) 49 | case .code: 50 | targetAttributeContainer.font = .monospacedSystemFont(ofSize: fontSize, weight: .regular) 51 | targetAttributeContainer.backgroundColor = .secondarySystemBackground 52 | case .strikethrough: 53 | targetAttributeContainer.strikethroughStyle = .single 54 | case .softBreak: 55 | break // TODO: Implement 56 | case .lineBreak: 57 | break // TODO: Implement 58 | case .inlineHTML: 59 | break // TODO: Implement 60 | case .blockHTML: 61 | break // TODO: Implement 62 | default: 63 | break 64 | } 65 | 66 | s = s.replacingAttributes(sourceAttributeContainer, with: targetAttributeContainer) 67 | } 68 | 69 | // MARK: Blocks 70 | 71 | // Accessing via dynamic lookup key path (\.presentationIntent) triggers a warning on Xcode 13.1, so we use the verbose way: AttributeScopes.FoundationAttributes.PresentationIntentAttribute.self 72 | 73 | // We use .reversed() iteration to be able to add characters to the string without breaking ranges. 74 | 75 | var previousListID = 0 76 | 77 | for (intentBlock, intentRange) in s.runs[AttributeScopes.FoundationAttributes.PresentationIntentAttribute.self].reversed() { 78 | guard let intentBlock = intentBlock else { continue } 79 | 80 | var block: MarkdownStyledBlock = .generic 81 | var currentElementOrdinal: Int = 0 82 | 83 | var currentListID = 0 84 | 85 | for intent in intentBlock.components { 86 | switch intent.kind { 87 | case .paragraph: 88 | if block == .generic { 89 | block = .paragraph 90 | } 91 | case .header(level: let level): 92 | block = .headline(level) 93 | case .orderedList: 94 | block = .orderedListElement(currentElementOrdinal) 95 | currentListID = intent.identity 96 | case .unorderedList: 97 | block = .unorderedListElement 98 | currentListID = intent.identity 99 | case .listItem(ordinal: let ordinal): 100 | currentElementOrdinal = ordinal 101 | if block != .unorderedListElement { 102 | block = .orderedListElement(ordinal) 103 | } 104 | case .codeBlock(languageHint: let languageHint): 105 | block = .code(languageHint) 106 | case .blockQuote: 107 | block = .blockquote 108 | case .thematicBreak: 109 | break // This is ---- in Markdown. 110 | case .table(columns: _): 111 | break 112 | case .tableHeaderRow: 113 | break 114 | case .tableRow(rowIndex: _): 115 | break 116 | case .tableCell(columnIndex: _): 117 | break 118 | @unknown default: 119 | break 120 | } 121 | } 122 | 123 | switch block { 124 | case .generic: 125 | assertionFailure(intentBlock.debugDescription) 126 | case .headline(let level): 127 | switch level { 128 | case 1: 129 | s[intentRange].font = .systemFont(ofSize: 30, weight: .heavy) 130 | case 2: 131 | s[intentRange].font = .systemFont(ofSize: 20, weight: .heavy) 132 | case 3: 133 | s[intentRange].font = .systemFont(ofSize: 15, weight: .heavy) 134 | default: 135 | // TODO: Handle H4 to H6 136 | s[intentRange].font = .systemFont(ofSize: 15, weight: .heavy) 137 | } 138 | case .paragraph: 139 | break 140 | case .unorderedListElement: 141 | s.characters.insert(contentsOf: "•\t", at: intentRange.lowerBound) 142 | s[intentRange].paragraphStyle = previousListID == currentListID ? listParagraphStyle : lastElementListParagraphStyle 143 | case .orderedListElement(let ordinal): 144 | s.characters.insert(contentsOf: "\(ordinal).\t", at: intentRange.lowerBound) 145 | s[intentRange].paragraphStyle = previousListID == currentListID ? listParagraphStyle : lastElementListParagraphStyle 146 | case .blockquote: 147 | s[intentRange].paragraphStyle = defaultParagraphStyle 148 | s[intentRange].foregroundColor = .secondaryLabel 149 | case .code: 150 | s[intentRange].font = .monospacedSystemFont(ofSize: 13, weight: .regular) 151 | s[intentRange].paragraphStyle = codeParagraphStyle 152 | } 153 | 154 | // Remember the list ID so we can check if it’s identical in the next block 155 | previousListID = currentListID 156 | 157 | // MARK: Add line breaks to separate blocks 158 | 159 | if intentRange.lowerBound != s.startIndex { 160 | s.characters.insert(contentsOf: "\n", at: intentRange.lowerBound) 161 | } 162 | } 163 | 164 | self = s 165 | } 166 | } 167 | 168 | fileprivate let defaultParagraphStyle: NSParagraphStyle = { 169 | var paragraphStyle = NSMutableParagraphStyle() 170 | paragraphStyle.paragraphSpacing = 10.0 171 | paragraphStyle.minimumLineHeight = 20.0 172 | return paragraphStyle 173 | }() 174 | 175 | 176 | fileprivate let listParagraphStyle: NSMutableParagraphStyle = { 177 | var paragraphStyle = NSMutableParagraphStyle() 178 | paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: 20)] 179 | paragraphStyle.headIndent = 20 180 | paragraphStyle.minimumLineHeight = 20.0 181 | return paragraphStyle 182 | }() 183 | 184 | fileprivate let lastElementListParagraphStyle: NSMutableParagraphStyle = { 185 | var paragraphStyle = NSMutableParagraphStyle() 186 | paragraphStyle.tabStops = [NSTextTab(textAlignment: .left, location: 20)] 187 | paragraphStyle.headIndent = 20 188 | paragraphStyle.minimumLineHeight = 20.0 189 | paragraphStyle.paragraphSpacing = 20.0 // The last element in a list needs extra paragraph spacing 190 | return paragraphStyle 191 | }() 192 | 193 | 194 | fileprivate let codeParagraphStyle: NSParagraphStyle = { 195 | var paragraphStyle = NSMutableParagraphStyle() 196 | paragraphStyle.minimumLineHeight = 20.0 197 | paragraphStyle.firstLineHeadIndent = 20 198 | paragraphStyle.headIndent = 20 199 | return paragraphStyle 200 | }() 201 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdown/Base.lproj/LaunchScreen.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdown/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 49 | 50 | 51 | 52 | TextView Lorem ipsum dolor sit er elit lamet, consectetaur cillium adipisicing pecu, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. Nam liber te conscient to factor tum poen legum odioque civiuda. 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 67 | 73 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdown/DrawView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Frank Rausch on 2021-11-15. 3 | // 4 | 5 | import UIKit 6 | 7 | // This is just a view that draws an NSAttributedString. 8 | class DrawView: UIView { 9 | var attributedText: NSAttributedString? { 10 | didSet { 11 | setNeedsDisplay() 12 | } 13 | } 14 | 15 | override func draw(_ rect: CGRect) { 16 | attributedText?.draw(in: bounds) 17 | } 18 | 19 | override func layoutSubviews() { 20 | super.layoutSubviews() 21 | setNeedsDisplay() 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdown/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | UIApplicationSceneManifest 6 | 7 | UIApplicationSupportsMultipleScenes 8 | 9 | UISceneConfigurations 10 | 11 | UIWindowSceneSessionRoleApplication 12 | 13 | 14 | UISceneConfigurationName 15 | Default Configuration 16 | UISceneDelegateClassName 17 | $(PRODUCT_MODULE_NAME).SceneDelegate 18 | UISceneStoryboardFile 19 | Main 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdown/SceneDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Frank Rausch on 2021-11-13. 3 | // 4 | 5 | 6 | import UIKit 7 | 8 | class SceneDelegate: UIResponder, UIWindowSceneDelegate { 9 | 10 | var window: UIWindow? 11 | 12 | 13 | func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) { 14 | // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`. 15 | // If using a storyboard, the `window` property will automatically be initialized and attached to the scene. 16 | // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead). 17 | guard let _ = (scene as? UIWindowScene) else { return } 18 | } 19 | 20 | func sceneDidDisconnect(_ scene: UIScene) { 21 | // Called as the scene is being released by the system. 22 | // This occurs shortly after the scene enters the background, or when its session is discarded. 23 | // Release any resources associated with this scene that can be re-created the next time the scene connects. 24 | // The scene may re-connect later, as its session was not necessarily discarded (see `application:didDiscardSceneSessions` instead). 25 | } 26 | 27 | func sceneDidBecomeActive(_ scene: UIScene) { 28 | // Called when the scene has moved from an inactive state to an active state. 29 | // Use this method to restart any tasks that were paused (or not yet started) when the scene was inactive. 30 | } 31 | 32 | func sceneWillResignActive(_ scene: UIScene) { 33 | // Called when the scene will move from an active state to an inactive state. 34 | // This may occur due to temporary interruptions (ex. an incoming phone call). 35 | } 36 | 37 | func sceneWillEnterForeground(_ scene: UIScene) { 38 | // Called as the scene transitions from the background to the foreground. 39 | // Use this method to undo the changes made on entering the background. 40 | } 41 | 42 | func sceneDidEnterBackground(_ scene: UIScene) { 43 | // Called as the scene transitions from the foreground to the background. 44 | // Use this method to save data, release shared resources, and store enough scene-specific state information 45 | // to restore the scene back to its current state. 46 | } 47 | 48 | 49 | } 50 | 51 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdown/ViewController.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Frank Rausch on 2021-11-13. 3 | // 4 | 5 | 6 | import UIKit 7 | 8 | class ViewController: UIViewController { 9 | 10 | @IBOutlet weak var drawView: DrawView! 11 | @IBOutlet weak var label: UILabel! 12 | @IBOutlet weak var textView: UITextView! 13 | 14 | @IBOutlet weak var segmentedControl: UISegmentedControl! 15 | 16 | override func viewDidLoad() { 17 | super.viewDidLoad() 18 | // Do any additional setup after loading the view. 19 | } 20 | 21 | override func viewWillAppear(_ animated: Bool) { 22 | super.viewWillAppear(animated) 23 | updateAttributedString() 24 | } 25 | 26 | @IBAction func segmentedControlDidChange(_ sender: Any) { 27 | updateAttributedString() 28 | } 29 | 30 | private func updateAttributedString() { 31 | 32 | let markdownString = """ 33 | # Headline 1 34 | ## Headline 2 35 | 36 | Here is an [Example Link](https://example.com). Lorem ipsum **bold** dolor sit _italic_ amet, consectetur **adipisicing** elit. 37 | 38 | - List item 1 lorem ipsum dolor sit amet lorem ipsum dolor 39 | - List item 2 40 | 41 | 1. List item 1 42 | 1. List item 2 43 | 1. List item 2 44 | 45 | ### Headline 3 46 | 47 | Ut enim ad `inline code` minim veniam, ~~strikethrough~~ quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. 48 | 49 | > Blockquote loorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. 50 | 51 | ``` 52 | 5 LET S = 0 53 | 10 MAT INPUT V 54 | 20 LET N = NUM 55 | 30 IF N = 0 THEN 99 56 | 40 FOR I = 1 TO N 57 | 45 LET S = S + V(I) 58 | 50 NEXT I 59 | 60 PRINT S/N 60 | 70 GO TO 5 61 | 99 END 62 | ``` 63 | 64 | """ 65 | 66 | let defaultMarkdown = try! AttributedString(markdown: markdownString) 67 | let styledMarkdown = try! AttributedString(styledMarkdown: markdownString) 68 | 69 | let attributed = segmentedControl.selectedSegmentIndex == 0 ? defaultMarkdown : styledMarkdown 70 | 71 | drawView.attributedText = NSAttributedString(attributed) 72 | label.attributedText = NSAttributedString(attributed) 73 | textView.attributedText = NSAttributedString(attributed) 74 | } 75 | 76 | } 77 | 78 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdownTests/AttributedStringStyledMarkdownTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Frank Rausch on 2021-11-18. 3 | // 4 | 5 | 6 | import XCTest 7 | @testable import AttributedStringStyledMarkdown 8 | 9 | class AttributedStringStyledMarkdownTests: XCTestCase { 10 | 11 | override func setUpWithError() throws { 12 | // Put setup code here. This method is called before the invocation of each test method in the class. 13 | } 14 | 15 | override func tearDownWithError() throws { 16 | // Put teardown code here. This method is called after the invocation of each test method in the class. 17 | } 18 | 19 | func testExample() throws { 20 | // This is an example of a functional test case. 21 | // Use XCTAssert and related functions to verify your tests produce the correct results. 22 | } 23 | 24 | func testPerformanceExample() throws { 25 | // This is an example of a performance test case. 26 | self.measure { 27 | // Put the code you want to measure the time of here. 28 | } 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdownUITests/AttributedStringStyledMarkdownUITests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Frank Rausch on 2021-11-18. 3 | // 4 | 5 | 6 | import XCTest 7 | 8 | class AttributedStringStyledMarkdownUITests: XCTestCase { 9 | 10 | override func setUpWithError() throws { 11 | // Put setup code here. This method is called before the invocation of each test method in the class. 12 | 13 | // In UI tests it is usually best to stop immediately when a failure occurs. 14 | continueAfterFailure = false 15 | 16 | // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. 17 | } 18 | 19 | override func tearDownWithError() throws { 20 | // Put teardown code here. This method is called after the invocation of each test method in the class. 21 | } 22 | 23 | func testExample() throws { 24 | // UI tests must launch the application that they test. 25 | let app = XCUIApplication() 26 | app.launch() 27 | 28 | // Use recording to get started writing UI tests. 29 | // Use XCTAssert and related functions to verify your tests produce the correct results. 30 | } 31 | 32 | func testLaunchPerformance() throws { 33 | if #available(macOS 10.15, iOS 13.0, tvOS 13.0, watchOS 7.0, *) { 34 | // This measures how long it takes to launch your application. 35 | measure(metrics: [XCTApplicationLaunchMetric()]) { 36 | XCUIApplication().launch() 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /AttributedStringStyledMarkdownUITests/AttributedStringStyledMarkdownUITestsLaunchTests.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Created by Frank Rausch on 2021-11-18. 3 | // 4 | 5 | 6 | import XCTest 7 | 8 | class AttributedStringStyledMarkdownUITestsLaunchTests: XCTestCase { 9 | 10 | override class var runsForEachTargetApplicationUIConfiguration: Bool { 11 | true 12 | } 13 | 14 | override func setUpWithError() throws { 15 | continueAfterFailure = false 16 | } 17 | 18 | func testLaunch() throws { 19 | let app = XCUIApplication() 20 | app.launch() 21 | 22 | // Insert steps here to perform after app launch but before taking a screenshot, 23 | // such as logging into a test account or navigating somewhere in the app 24 | 25 | let attachment = XCTAttachment(screenshot: app.screenshot()) 26 | attachment.name = "Launch Screen" 27 | attachment.lifetime = .keepAlways 28 | add(attachment) 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Frank Rausch 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AttributedString Markdown initializer with custom styling 2 | 3 | [`AttributedString`](https://developer.apple.com/documentation/foundation/attributedstring) in iOS 15 and macOS 12 comes with a [Markdown initializer](https://developer.apple.com/documentation/foundation/attributedstring/3796160-init). 4 | 5 | ---- 6 | 7 | But: 8 | 9 | - There is no styling (not even bold or italic) when [drawing](https://developer.apple.com/documentation/foundation/nsattributedstring/1524971-draw) the `AttributedString` in a custom view. 10 | - The `AttributedString` does have some styling applied when it’s assigned to a `UILabel` or to a `UITextView`, but they are noticeably different and weird things happen with the font sizes. 11 | - All line breaks that should separate the content blocks are missing. 12 | - The `AttributedString` colors don’t react to toggling between dark and light mode. 13 | 14 | ## What’s going on? 15 | 16 | There is not a lot of information about Markdown in `AttributedString` out there yet, but I think I was just [holding it wrong](https://www.urbandictionary.com/define.php?term=You%27re%20Holding%20It%20Wrong): 17 | 18 | The Markdown initializer merely adds *semantic* (not visual) markup to the `AttributedString`. Both `UILabel` and `UITextView` interpret these to a certain extent, while drawing a raw `(NS)AttributedString` doesn’t. 19 | 20 | The Markdown initializer merely puts the Markdown parsing results as [*Presentation Intents*](https://developer.apple.com/documentation/foundation/nspresentationintent) into the `AttributedString` runs. 21 | - There are `inlinePresentationIntent`s, e.g. `.stronglyEmphasized` for bold text. 22 | - There are `presentationIntent`s for blocks like headlines (like `.header(1)` for a `H1`). 23 | 24 | ## This sample project 25 | 26 | This sample project shows how you can find these intents and replace them with actual `AttributedString` styling information like font weights and foreground colors. 27 | 28 | I don’t know if there’s a more elegant way to solve this. 29 | Do let me know if you have a better solution! 30 | 31 | ![AttributedString Markdown Light](attributedstring-markdown-light.jpg) 32 | 33 | ![AttributedString Markdown Dark](attributedstring-markdown-dark.jpg) 34 | 35 | ---- 36 | 37 | © 2021 [Frank Rausch](https://twitter.com/frankrausch) 38 | -------------------------------------------------------------------------------- /attributedstring-markdown-dark.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankrausch/AttributedStringStyledMarkdown/b458d5c77eaac3587a39d8c7ba3312679603eeb4/attributedstring-markdown-dark.jpg -------------------------------------------------------------------------------- /attributedstring-markdown-light.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/frankrausch/AttributedStringStyledMarkdown/b458d5c77eaac3587a39d8c7ba3312679603eeb4/attributedstring-markdown-light.jpg --------------------------------------------------------------------------------