├── README.md ├── STTextView.xcodeproj ├── project.pbxproj ├── project.xcworkspace │ └── contents.xcworkspacedata └── xcuserdata │ └── StriEver.xcuserdatad │ ├── xcdebugger │ └── Breakpoints_v2.xcbkptlist │ └── xcschemes │ ├── STTextView.xcscheme │ └── xcschememanagement.plist └── STTextView ├── AppDelegate.h ├── AppDelegate.m ├── Assets.xcassets └── AppIcon.appiconset │ └── Contents.json ├── Base.lproj ├── LaunchScreen.storyboard └── Main.storyboard ├── Info.plist ├── STTextView ├── STTextView.h └── STTextView.m ├── UIViewExtention ├── UIView+Extension.h └── UIView+Extension.m ├── ViewController.h ├── ViewController.m └── main.m /README.md: -------------------------------------------------------------------------------- 1 | # STTextView 2 | ##我们知道iOS自带的UITextView基本很难满足我们的日常使用。我项目中textView是继承与UITextview的,自定义了一个TextView,主要实现的以下功能。 3 | 1. 添加placeHolder 4 | 2. 高度自适应 5 | 3. 可以设置行间距 6 | 4. 支持xib 7 | 看一下实际使用效果吧: 8 | ![实际使用效果](http://upload-images.jianshu.io/upload_images/1663049-631fb8cb7158d9ac.gif?imageMogr2/auto-orient/strip) 9 | 希望能够帮助到大家吧,下载拖进工程即可使用,如果使用中有什么问题请及时与我联系。 10 | -------------------------------------------------------------------------------- /STTextView.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 46; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | D3A184811ED2D54600447F87 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = D3A184801ED2D54600447F87 /* main.m */; }; 11 | D3A184841ED2D54600447F87 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = D3A184831ED2D54600447F87 /* AppDelegate.m */; }; 12 | D3A184871ED2D54600447F87 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = D3A184861ED2D54600447F87 /* ViewController.m */; }; 13 | D3A1848A1ED2D54600447F87 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D3A184881ED2D54600447F87 /* Main.storyboard */; }; 14 | D3A1848C1ED2D54700447F87 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = D3A1848B1ED2D54700447F87 /* Assets.xcassets */; }; 15 | D3A1848F1ED2D54700447F87 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D3A1848D1ED2D54700447F87 /* LaunchScreen.storyboard */; }; 16 | D3A1849C1ED2FC0000447F87 /* UIView+Extension.m in Sources */ = {isa = PBXBuildFile; fileRef = D3A1849B1ED2FC0000447F87 /* UIView+Extension.m */; }; 17 | D3D470661ED4239D00C15365 /* STTextView.m in Sources */ = {isa = PBXBuildFile; fileRef = D3D470651ED4239D00C15365 /* STTextView.m */; }; 18 | /* End PBXBuildFile section */ 19 | 20 | /* Begin PBXFileReference section */ 21 | D3A1847C1ED2D54600447F87 /* STTextView.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = STTextView.app; sourceTree = BUILT_PRODUCTS_DIR; }; 22 | D3A184801ED2D54600447F87 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; 23 | D3A184821ED2D54600447F87 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 24 | D3A184831ED2D54600447F87 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 25 | D3A184851ED2D54600447F87 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; 26 | D3A184861ED2D54600447F87 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; 27 | D3A184891ED2D54600447F87 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 28 | D3A1848B1ED2D54700447F87 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 29 | D3A1848E1ED2D54700447F87 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; 30 | D3A184901ED2D54700447F87 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 31 | D3A1849A1ED2FC0000447F87 /* UIView+Extension.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+Extension.h"; sourceTree = ""; }; 32 | D3A1849B1ED2FC0000447F87 /* UIView+Extension.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+Extension.m"; sourceTree = ""; }; 33 | D3D470641ED4239D00C15365 /* STTextView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = STTextView.h; sourceTree = ""; }; 34 | D3D470651ED4239D00C15365 /* STTextView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = STTextView.m; sourceTree = ""; }; 35 | /* End PBXFileReference section */ 36 | 37 | /* Begin PBXFrameworksBuildPhase section */ 38 | D3A184791ED2D54600447F87 /* Frameworks */ = { 39 | isa = PBXFrameworksBuildPhase; 40 | buildActionMask = 2147483647; 41 | files = ( 42 | ); 43 | runOnlyForDeploymentPostprocessing = 0; 44 | }; 45 | /* End PBXFrameworksBuildPhase section */ 46 | 47 | /* Begin PBXGroup section */ 48 | D3A184731ED2D54600447F87 = { 49 | isa = PBXGroup; 50 | children = ( 51 | D3A1847E1ED2D54600447F87 /* STTextView */, 52 | D3A1847D1ED2D54600447F87 /* Products */, 53 | ); 54 | sourceTree = ""; 55 | }; 56 | D3A1847D1ED2D54600447F87 /* Products */ = { 57 | isa = PBXGroup; 58 | children = ( 59 | D3A1847C1ED2D54600447F87 /* STTextView.app */, 60 | ); 61 | name = Products; 62 | sourceTree = ""; 63 | }; 64 | D3A1847E1ED2D54600447F87 /* STTextView */ = { 65 | isa = PBXGroup; 66 | children = ( 67 | D3D470631ED4239D00C15365 /* STTextView */, 68 | D3A184991ED2FC0000447F87 /* UIViewExtention */, 69 | D3A184821ED2D54600447F87 /* AppDelegate.h */, 70 | D3A184831ED2D54600447F87 /* AppDelegate.m */, 71 | D3A184851ED2D54600447F87 /* ViewController.h */, 72 | D3A184861ED2D54600447F87 /* ViewController.m */, 73 | D3A184881ED2D54600447F87 /* Main.storyboard */, 74 | D3A1848B1ED2D54700447F87 /* Assets.xcassets */, 75 | D3A1848D1ED2D54700447F87 /* LaunchScreen.storyboard */, 76 | D3A184901ED2D54700447F87 /* Info.plist */, 77 | D3A1847F1ED2D54600447F87 /* Supporting Files */, 78 | ); 79 | path = STTextView; 80 | sourceTree = ""; 81 | }; 82 | D3A1847F1ED2D54600447F87 /* Supporting Files */ = { 83 | isa = PBXGroup; 84 | children = ( 85 | D3A184801ED2D54600447F87 /* main.m */, 86 | ); 87 | name = "Supporting Files"; 88 | sourceTree = ""; 89 | }; 90 | D3A184991ED2FC0000447F87 /* UIViewExtention */ = { 91 | isa = PBXGroup; 92 | children = ( 93 | D3A1849A1ED2FC0000447F87 /* UIView+Extension.h */, 94 | D3A1849B1ED2FC0000447F87 /* UIView+Extension.m */, 95 | ); 96 | path = UIViewExtention; 97 | sourceTree = ""; 98 | }; 99 | D3D470631ED4239D00C15365 /* STTextView */ = { 100 | isa = PBXGroup; 101 | children = ( 102 | D3D470641ED4239D00C15365 /* STTextView.h */, 103 | D3D470651ED4239D00C15365 /* STTextView.m */, 104 | ); 105 | path = STTextView; 106 | sourceTree = ""; 107 | }; 108 | /* End PBXGroup section */ 109 | 110 | /* Begin PBXNativeTarget section */ 111 | D3A1847B1ED2D54600447F87 /* STTextView */ = { 112 | isa = PBXNativeTarget; 113 | buildConfigurationList = D3A184931ED2D54700447F87 /* Build configuration list for PBXNativeTarget "STTextView" */; 114 | buildPhases = ( 115 | D3A184781ED2D54600447F87 /* Sources */, 116 | D3A184791ED2D54600447F87 /* Frameworks */, 117 | D3A1847A1ED2D54600447F87 /* Resources */, 118 | ); 119 | buildRules = ( 120 | ); 121 | dependencies = ( 122 | ); 123 | name = STTextView; 124 | productName = STTextView; 125 | productReference = D3A1847C1ED2D54600447F87 /* STTextView.app */; 126 | productType = "com.apple.product-type.application"; 127 | }; 128 | /* End PBXNativeTarget section */ 129 | 130 | /* Begin PBXProject section */ 131 | D3A184741ED2D54600447F87 /* Project object */ = { 132 | isa = PBXProject; 133 | attributes = { 134 | LastUpgradeCheck = 0820; 135 | ORGANIZATIONNAME = StriEver; 136 | TargetAttributes = { 137 | D3A1847B1ED2D54600447F87 = { 138 | CreatedOnToolsVersion = 8.2.1; 139 | DevelopmentTeam = 5B83538JM2; 140 | ProvisioningStyle = Automatic; 141 | }; 142 | }; 143 | }; 144 | buildConfigurationList = D3A184771ED2D54600447F87 /* Build configuration list for PBXProject "STTextView" */; 145 | compatibilityVersion = "Xcode 3.2"; 146 | developmentRegion = English; 147 | hasScannedForEncodings = 0; 148 | knownRegions = ( 149 | en, 150 | Base, 151 | ); 152 | mainGroup = D3A184731ED2D54600447F87; 153 | productRefGroup = D3A1847D1ED2D54600447F87 /* Products */; 154 | projectDirPath = ""; 155 | projectRoot = ""; 156 | targets = ( 157 | D3A1847B1ED2D54600447F87 /* STTextView */, 158 | ); 159 | }; 160 | /* End PBXProject section */ 161 | 162 | /* Begin PBXResourcesBuildPhase section */ 163 | D3A1847A1ED2D54600447F87 /* Resources */ = { 164 | isa = PBXResourcesBuildPhase; 165 | buildActionMask = 2147483647; 166 | files = ( 167 | D3A1848F1ED2D54700447F87 /* LaunchScreen.storyboard in Resources */, 168 | D3A1848C1ED2D54700447F87 /* Assets.xcassets in Resources */, 169 | D3A1848A1ED2D54600447F87 /* Main.storyboard in Resources */, 170 | ); 171 | runOnlyForDeploymentPostprocessing = 0; 172 | }; 173 | /* End PBXResourcesBuildPhase section */ 174 | 175 | /* Begin PBXSourcesBuildPhase section */ 176 | D3A184781ED2D54600447F87 /* Sources */ = { 177 | isa = PBXSourcesBuildPhase; 178 | buildActionMask = 2147483647; 179 | files = ( 180 | D3A1849C1ED2FC0000447F87 /* UIView+Extension.m in Sources */, 181 | D3A184871ED2D54600447F87 /* ViewController.m in Sources */, 182 | D3A184841ED2D54600447F87 /* AppDelegate.m in Sources */, 183 | D3D470661ED4239D00C15365 /* STTextView.m in Sources */, 184 | D3A184811ED2D54600447F87 /* main.m in Sources */, 185 | ); 186 | runOnlyForDeploymentPostprocessing = 0; 187 | }; 188 | /* End PBXSourcesBuildPhase section */ 189 | 190 | /* Begin PBXVariantGroup section */ 191 | D3A184881ED2D54600447F87 /* Main.storyboard */ = { 192 | isa = PBXVariantGroup; 193 | children = ( 194 | D3A184891ED2D54600447F87 /* Base */, 195 | ); 196 | name = Main.storyboard; 197 | sourceTree = ""; 198 | }; 199 | D3A1848D1ED2D54700447F87 /* LaunchScreen.storyboard */ = { 200 | isa = PBXVariantGroup; 201 | children = ( 202 | D3A1848E1ED2D54700447F87 /* Base */, 203 | ); 204 | name = LaunchScreen.storyboard; 205 | sourceTree = ""; 206 | }; 207 | /* End PBXVariantGroup section */ 208 | 209 | /* Begin XCBuildConfiguration section */ 210 | D3A184911ED2D54700447F87 /* Debug */ = { 211 | isa = XCBuildConfiguration; 212 | buildSettings = { 213 | ALWAYS_SEARCH_USER_PATHS = NO; 214 | CLANG_ANALYZER_NONNULL = YES; 215 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 216 | CLANG_CXX_LIBRARY = "libc++"; 217 | CLANG_ENABLE_MODULES = YES; 218 | CLANG_ENABLE_OBJC_ARC = YES; 219 | CLANG_WARN_BOOL_CONVERSION = YES; 220 | CLANG_WARN_CONSTANT_CONVERSION = YES; 221 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 222 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 223 | CLANG_WARN_EMPTY_BODY = YES; 224 | CLANG_WARN_ENUM_CONVERSION = YES; 225 | CLANG_WARN_INFINITE_RECURSION = YES; 226 | CLANG_WARN_INT_CONVERSION = YES; 227 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 228 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 229 | CLANG_WARN_UNREACHABLE_CODE = YES; 230 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 231 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 232 | COPY_PHASE_STRIP = NO; 233 | DEBUG_INFORMATION_FORMAT = dwarf; 234 | ENABLE_STRICT_OBJC_MSGSEND = YES; 235 | ENABLE_TESTABILITY = YES; 236 | GCC_C_LANGUAGE_STANDARD = gnu99; 237 | GCC_DYNAMIC_NO_PIC = NO; 238 | GCC_NO_COMMON_BLOCKS = YES; 239 | GCC_OPTIMIZATION_LEVEL = 0; 240 | GCC_PREPROCESSOR_DEFINITIONS = ( 241 | "DEBUG=1", 242 | "$(inherited)", 243 | ); 244 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 245 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 246 | GCC_WARN_UNDECLARED_SELECTOR = YES; 247 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 248 | GCC_WARN_UNUSED_FUNCTION = YES; 249 | GCC_WARN_UNUSED_VARIABLE = YES; 250 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 251 | MTL_ENABLE_DEBUG_INFO = YES; 252 | ONLY_ACTIVE_ARCH = YES; 253 | SDKROOT = iphoneos; 254 | }; 255 | name = Debug; 256 | }; 257 | D3A184921ED2D54700447F87 /* Release */ = { 258 | isa = XCBuildConfiguration; 259 | buildSettings = { 260 | ALWAYS_SEARCH_USER_PATHS = NO; 261 | CLANG_ANALYZER_NONNULL = YES; 262 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; 263 | CLANG_CXX_LIBRARY = "libc++"; 264 | CLANG_ENABLE_MODULES = YES; 265 | CLANG_ENABLE_OBJC_ARC = YES; 266 | CLANG_WARN_BOOL_CONVERSION = YES; 267 | CLANG_WARN_CONSTANT_CONVERSION = YES; 268 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 269 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 270 | CLANG_WARN_EMPTY_BODY = YES; 271 | CLANG_WARN_ENUM_CONVERSION = YES; 272 | CLANG_WARN_INFINITE_RECURSION = YES; 273 | CLANG_WARN_INT_CONVERSION = YES; 274 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 275 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 276 | CLANG_WARN_UNREACHABLE_CODE = YES; 277 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 278 | "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; 279 | COPY_PHASE_STRIP = NO; 280 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 281 | ENABLE_NS_ASSERTIONS = NO; 282 | ENABLE_STRICT_OBJC_MSGSEND = YES; 283 | GCC_C_LANGUAGE_STANDARD = gnu99; 284 | GCC_NO_COMMON_BLOCKS = YES; 285 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 286 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 287 | GCC_WARN_UNDECLARED_SELECTOR = YES; 288 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 289 | GCC_WARN_UNUSED_FUNCTION = YES; 290 | GCC_WARN_UNUSED_VARIABLE = YES; 291 | IPHONEOS_DEPLOYMENT_TARGET = 10.2; 292 | MTL_ENABLE_DEBUG_INFO = NO; 293 | SDKROOT = iphoneos; 294 | VALIDATE_PRODUCT = YES; 295 | }; 296 | name = Release; 297 | }; 298 | D3A184941ED2D54700447F87 /* Debug */ = { 299 | isa = XCBuildConfiguration; 300 | buildSettings = { 301 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 302 | DEVELOPMENT_TEAM = 5B83538JM2; 303 | INFOPLIST_FILE = STTextView/Info.plist; 304 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 305 | PRODUCT_BUNDLE_IDENTIFIER = com.igancao.www.STTextView; 306 | PRODUCT_NAME = "$(TARGET_NAME)"; 307 | }; 308 | name = Debug; 309 | }; 310 | D3A184951ED2D54700447F87 /* Release */ = { 311 | isa = XCBuildConfiguration; 312 | buildSettings = { 313 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 314 | DEVELOPMENT_TEAM = 5B83538JM2; 315 | INFOPLIST_FILE = STTextView/Info.plist; 316 | LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; 317 | PRODUCT_BUNDLE_IDENTIFIER = com.igancao.www.STTextView; 318 | PRODUCT_NAME = "$(TARGET_NAME)"; 319 | }; 320 | name = Release; 321 | }; 322 | /* End XCBuildConfiguration section */ 323 | 324 | /* Begin XCConfigurationList section */ 325 | D3A184771ED2D54600447F87 /* Build configuration list for PBXProject "STTextView" */ = { 326 | isa = XCConfigurationList; 327 | buildConfigurations = ( 328 | D3A184911ED2D54700447F87 /* Debug */, 329 | D3A184921ED2D54700447F87 /* Release */, 330 | ); 331 | defaultConfigurationIsVisible = 0; 332 | defaultConfigurationName = Release; 333 | }; 334 | D3A184931ED2D54700447F87 /* Build configuration list for PBXNativeTarget "STTextView" */ = { 335 | isa = XCConfigurationList; 336 | buildConfigurations = ( 337 | D3A184941ED2D54700447F87 /* Debug */, 338 | D3A184951ED2D54700447F87 /* Release */, 339 | ); 340 | defaultConfigurationIsVisible = 0; 341 | defaultConfigurationName = Release; 342 | }; 343 | /* End XCConfigurationList section */ 344 | }; 345 | rootObject = D3A184741ED2D54600447F87 /* Project object */; 346 | } 347 | -------------------------------------------------------------------------------- /STTextView.xcodeproj/project.xcworkspace/contents.xcworkspacedata: -------------------------------------------------------------------------------- 1 | 2 | 4 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /STTextView.xcodeproj/xcuserdata/StriEver.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 8 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /STTextView.xcodeproj/xcuserdata/StriEver.xcuserdatad/xcschemes/STTextView.xcscheme: -------------------------------------------------------------------------------- 1 | 2 | 5 | 8 | 9 | 15 | 21 | 22 | 23 | 24 | 25 | 30 | 31 | 32 | 33 | 39 | 40 | 41 | 42 | 43 | 44 | 54 | 56 | 62 | 63 | 64 | 65 | 66 | 67 | 73 | 75 | 81 | 82 | 83 | 84 | 86 | 87 | 90 | 91 | 92 | -------------------------------------------------------------------------------- /STTextView.xcodeproj/xcuserdata/StriEver.xcuserdatad/xcschemes/xcschememanagement.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | SchemeUserState 6 | 7 | STTextView.xcscheme 8 | 9 | orderHint 10 | 0 11 | 12 | 13 | SuppressBuildableAutocreation 14 | 15 | D3A1847B1ED2D54600447F87 16 | 17 | primary 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /STTextView/AppDelegate.h: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.h 3 | // STTextView 4 | // 5 | // Created by StriEver on 17/5/22. 6 | // Copyright © 2017年 StriEver. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface AppDelegate : UIResponder 12 | 13 | @property (strong, nonatomic) UIWindow *window; 14 | 15 | 16 | @end 17 | 18 | -------------------------------------------------------------------------------- /STTextView/AppDelegate.m: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.m 3 | // STTextView 4 | // 5 | // Created by StriEver on 17/5/22. 6 | // Copyright © 2017年 StriEver. All rights reserved. 7 | // 8 | 9 | #import "AppDelegate.h" 10 | 11 | @interface AppDelegate () 12 | 13 | @end 14 | 15 | @implementation AppDelegate 16 | 17 | 18 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 19 | // Override point for customization after application launch. 20 | return YES; 21 | } 22 | 23 | 24 | - (void)applicationWillResignActive:(UIApplication *)application { 25 | // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. 26 | // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game. 27 | } 28 | 29 | 30 | - (void)applicationDidEnterBackground:(UIApplication *)application { 31 | // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 32 | // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. 33 | } 34 | 35 | 36 | - (void)applicationWillEnterForeground:(UIApplication *)application { 37 | // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background. 38 | } 39 | 40 | 41 | - (void)applicationDidBecomeActive:(UIApplication *)application { 42 | // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. 43 | } 44 | 45 | 46 | - (void)applicationWillTerminate:(UIApplication *)application { 47 | // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 48 | } 49 | 50 | 51 | @end 52 | -------------------------------------------------------------------------------- /STTextView/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "iphone", 5 | "size" : "29x29", 6 | "scale" : "2x" 7 | }, 8 | { 9 | "idiom" : "iphone", 10 | "size" : "29x29", 11 | "scale" : "3x" 12 | }, 13 | { 14 | "idiom" : "iphone", 15 | "size" : "40x40", 16 | "scale" : "2x" 17 | }, 18 | { 19 | "idiom" : "iphone", 20 | "size" : "40x40", 21 | "scale" : "3x" 22 | }, 23 | { 24 | "idiom" : "iphone", 25 | "size" : "60x60", 26 | "scale" : "2x" 27 | }, 28 | { 29 | "idiom" : "iphone", 30 | "size" : "60x60", 31 | "scale" : "3x" 32 | } 33 | ], 34 | "info" : { 35 | "version" : 1, 36 | "author" : "xcode" 37 | } 38 | } -------------------------------------------------------------------------------- /STTextView/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 | 27 | 28 | -------------------------------------------------------------------------------- /STTextView/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 | -------------------------------------------------------------------------------- /STTextView/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | en 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | APPL 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | 1 21 | LSRequiresIPhoneOS 22 | 23 | UILaunchStoryboardName 24 | LaunchScreen 25 | UIMainStoryboardFile 26 | Main 27 | UIRequiredDeviceCapabilities 28 | 29 | armv7 30 | 31 | UISupportedInterfaceOrientations 32 | 33 | UIInterfaceOrientationPortrait 34 | UIInterfaceOrientationLandscapeLeft 35 | UIInterfaceOrientationLandscapeRight 36 | 37 | 38 | 39 | -------------------------------------------------------------------------------- /STTextView/STTextView/STTextView.h: -------------------------------------------------------------------------------- 1 | // 2 | // STTextView.h 3 | // STTextView 4 | // 5 | // Created by StriEver on 17/5/22. 6 | // Copyright © 2017年 StriEver. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface STTextView : UITextView 12 | /** 占位文字 */ 13 | @property (nonatomic, copy) NSString *placeholder; 14 | /** 占位文字颜色 */ 15 | @property (nonatomic, strong) UIColor *placeholderColor; 16 | //行间距 17 | @property (nonatomic, assign) CGFloat verticalSpacing; 18 | /**设置最大高度*/ 19 | @property (nonatomic, assign) CGFloat maxHeight; 20 | /**设置最小高度*/ 21 | @property (nonatomic, assign) CGFloat minHeight; 22 | /**是不是自适应高度,默认为YES*/ 23 | @property (nonatomic, assign) BOOL isAutoHeight; 24 | 25 | @property (nonatomic, copy) void(^textDidChangedBlock)(NSString * text); 26 | @property (nonatomic, copy) void (^textViewAutoHeight)(CGFloat textHeight); 27 | @end 28 | -------------------------------------------------------------------------------- /STTextView/STTextView/STTextView.m: -------------------------------------------------------------------------------- 1 | // 2 | // STTextView.m 3 | // STTextView 4 | // 5 | // Created by StriEver on 17/5/22. 6 | // Copyright © 2017年 StriEver. All rights reserved. 7 | // 8 | 9 | #import "STTextView.h" 10 | #import "UIView+Extension.h" 11 | #define DefaultColor [UIColor grayColor] 12 | @interface STTextView () 13 | @property (nonatomic, strong) UILabel * placeHolderLabel; 14 | @property (nonatomic, assign) UIEdgeInsets placeHolderLabelInsets; 15 | @end 16 | @implementation STTextView 17 | - (void)dealloc{ 18 | [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidChangeNotification object:nil]; 19 | [[NSNotificationCenter defaultCenter]removeObserver:self name:UITextViewTextDidEndEditingNotification object:nil]; 20 | } 21 | - (instancetype)initWithCoder:(NSCoder *)aDecoder{ 22 | if (self = [super initWithCoder:aDecoder]) { 23 | [self setUpView]; 24 | } 25 | return self; 26 | } 27 | - (instancetype)initWithFrame:(CGRect)frame{ 28 | if (self = [super initWithFrame:frame]) { 29 | [self setUpView]; 30 | } 31 | return self; 32 | } 33 | #pragma mark ---setter 34 | - (void)setTextContainerInset:(UIEdgeInsets)textContainerInset{ 35 | //调整text内容边距 36 | [super setTextContainerInset:textContainerInset]; 37 | self.placeHolderLabelInsets = UIEdgeInsetsMake(textContainerInset.top, textContainerInset.left + 2, textContainerInset.bottom, textContainerInset.right + 2); 38 | [self setNeedsLayout]; 39 | } 40 | - (void)setText:(NSString *)text{ 41 | [super setText:text]; 42 | [self textViewDidChange:nil]; 43 | } 44 | - (void)setFont:(UIFont *)font{ 45 | [super setFont:font]; 46 | self.placeHolderLabel.font = font; 47 | } 48 | - (void)setPlaceholder:(NSString *)placeholder{ 49 | _placeholder = placeholder; 50 | self.placeHolderLabel.text = _placeholder; 51 | } 52 | - (void)setPlaceholderColor:(UIColor *)placeholderColor{ 53 | _placeholderColor = placeholderColor; 54 | self.placeHolderLabel.textColor = _placeholderColor; 55 | } 56 | - (void)setIsAutoHeight:(BOOL)isAutoHeight{ 57 | _isAutoHeight = isAutoHeight; 58 | if (_isAutoHeight) { 59 | self.scrollEnabled = NO; 60 | } 61 | } 62 | - (void)setPlaceHolderLabelInsets:(UIEdgeInsets)placeHolderLabelInsets{ 63 | _placeHolderLabelInsets = placeHolderLabelInsets; 64 | [self layoutSubviews]; 65 | } 66 | 67 | - (void)resetPlaceHolderLabelState{ 68 | if ([self hasText]) { 69 | self.placeHolderLabel.hidden = YES; 70 | }else{ 71 | self.placeHolderLabel.hidden = NO; 72 | } 73 | } 74 | #pragma mark ---UI 75 | - (void)setUpView{ 76 | [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewDidEndEditing:) name:UITextViewTextDidEndEditingNotification object:nil]; 77 | [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textViewDidChange:) name:UITextViewTextDidChangeNotification object:nil]; 78 | _placeHolderLabel = [UILabel new]; 79 | _placeHolderLabel.textColor = self.placeholderColor ? self.placeholderColor : DefaultColor; 80 | _placeHolderLabel.text = self.placeholder; 81 | _placeHolderLabel.font = self.font; 82 | _placeHolderLabel.numberOfLines = 0; 83 | [self addSubview:_placeHolderLabel]; 84 | 85 | [self setPlaceHolderLabelInsets:UIEdgeInsetsMake(8, 2, 8, 2)]; 86 | self.font = [UIFont systemFontOfSize:17]; 87 | self.isAutoHeight = YES; 88 | self.maxHeight = 300; 89 | self.minHeight = 30; 90 | NSLog(@"self.textContainerInset--->%@\n---------->%@\nself.scrollIndicatorInsets----------->%@",NSStringFromUIEdgeInsets(self.textContainerInset),NSStringFromUIEdgeInsets(self.contentInset),NSStringFromUIEdgeInsets(self.scrollIndicatorInsets)); 91 | } 92 | - (void)layoutSubviews{ 93 | [super layoutSubviews]; 94 | _placeHolderLabel.frame = CGRectMake(_placeHolderLabelInsets.left, _placeHolderLabelInsets.top, self.frame.size.width - _placeHolderLabelInsets.left - _placeHolderLabelInsets.right, 0); 95 | [_placeHolderLabel sizeToFit]; 96 | if (_placeHolderLabel.hidden == NO && self.isAutoHeight) { 97 | CGFloat placeHolderHeight = _placeHolderLabel.height + (_placeHolderLabelInsets.top + _placeHolderLabelInsets.bottom); 98 | if (placeHolderHeight > self.height) { 99 | self.height = placeHolderHeight; 100 | } 101 | } 102 | 103 | } 104 | 105 | #pragma mark ---NSNotificationEvent 106 | - (void)textViewDidEndEditing:(NSNotification *)notification{ 107 | 108 | } 109 | - (void)textViewDidChange:(NSNotification *)notification{ 110 | if (self.text.length == 0) { 111 | self.placeHolderLabel.text = _placeholder; 112 | self.placeHolderLabel.hidden = NO; 113 | [self setNeedsLayout]; 114 | }else{ 115 | self.placeHolderLabel.hidden = YES; 116 | } 117 | if(self.markedTextRange == nil){ 118 | //没有候选字符 119 | [self st_setAttributedString]; 120 | self.textDidChangedBlock ? self.textDidChangedBlock(self.text) : nil; 121 | }; 122 | 123 | [self st_autoHeight]; 124 | [self resetPlaceHolderLabelState]; 125 | 126 | } 127 | 128 | #pragma mark --- function 129 | //设置属性字符串 130 | - (void)st_setAttributedString{ 131 | //设置了间距 132 | if (self.verticalSpacing > 0) { 133 | if (self.text.length > 0) { 134 | NSRange range = self.selectedRange; 135 | self.attributedText = [[NSAttributedString alloc]initWithString:self.text attributes:[self attrs]]; 136 | self.selectedRange = range; 137 | } 138 | } 139 | } 140 | - (NSDictionary *)attrs{ 141 | if (self.verticalSpacing > 0) { 142 | NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init]; 143 | paragraphStyle.lineSpacing = self.verticalSpacing;// 字体的行间距 144 | NSDictionary *attributes = @{NSFontAttributeName:self.font,NSParagraphStyleAttributeName:paragraphStyle}; 145 | return attributes; 146 | } 147 | return nil; 148 | } 149 | 150 | - (void)st_autoHeight{ 151 | //是否开启 152 | CGFloat height = 0;; 153 | if (self.isAutoHeight) { 154 | if (self.text.length == 0){ 155 | height = self.minHeight; 156 | self.height = self.minHeight; 157 | //高度变化 158 | self.textViewAutoHeight ? self.textViewAutoHeight(self.height) : nil; 159 | }else{ 160 | height = [self att_height]; 161 | if (height != self.height) { 162 | if (height > self.maxHeight) { 163 | self.height = self.maxHeight; 164 | self.scrollEnabled = YES; 165 | }else if (height < self.minHeight){ 166 | self.height = self.minHeight; 167 | self.scrollEnabled = NO; 168 | }else{ 169 | self.scrollEnabled = NO; 170 | self.height = height; 171 | } 172 | //高度变化 173 | self.textViewAutoHeight ? self.textViewAutoHeight(self.height) : nil; 174 | } 175 | } 176 | } 177 | } 178 | 179 | //计算高度 180 | - (CGFloat)att_height{ 181 | @autoreleasepool{ 182 | UITextView * tempTV = [UITextView new]; 183 | tempTV.font = self.font; 184 | tempTV.textContainerInset = self.textContainerInset; 185 | if ([self attrs]) { 186 | tempTV.attributedText = [[NSAttributedString alloc]initWithString:self.text attributes:[self attrs]]; 187 | }else{ 188 | tempTV.text = self.text; 189 | } 190 | CGFloat height = [tempTV sizeThatFits:CGSizeMake(self.width, MAXFLOAT)].height; 191 | return height; 192 | } 193 | } 194 | @end 195 | -------------------------------------------------------------------------------- /STTextView/UIViewExtention/UIView+Extension.h: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+Extension.h 3 | // 4 | 5 | #import 6 | 7 | @interface UIView (Extension) 8 | @property (nonatomic, assign) CGFloat x; 9 | @property (nonatomic, assign) CGFloat y; 10 | @property (nonatomic, assign) CGFloat centerX; 11 | @property (nonatomic, assign) CGFloat centerY; 12 | @property (nonatomic, assign) CGFloat width; 13 | @property (nonatomic, assign) CGFloat height; 14 | @property (nonatomic, assign) CGSize size; 15 | 16 | @property (nonatomic) CGFloat left; ///< Shortcut for frame.origin.x. 17 | @property (nonatomic) CGFloat top; ///< Shortcut for frame.origin.y 18 | @property (nonatomic) CGFloat right; ///< Shortcut for frame.origin.x + frame.size.width 19 | @property (nonatomic) CGFloat bottom; 20 | @end 21 | -------------------------------------------------------------------------------- /STTextView/UIViewExtention/UIView+Extension.m: -------------------------------------------------------------------------------- 1 | // 2 | // UIView+Extension.m 3 | // 4 | 5 | #import "UIView+Extension.h" 6 | 7 | @implementation UIView (Extension) 8 | 9 | - (void)setX:(CGFloat)x 10 | { 11 | CGRect frame = self.frame; 12 | frame.origin.x = x; 13 | self.frame = frame; 14 | } 15 | 16 | - (CGFloat)x 17 | { 18 | return self.frame.origin.x; 19 | } 20 | 21 | - (void)setY:(CGFloat)y 22 | { 23 | CGRect frame = self.frame; 24 | frame.origin.y = y; 25 | self.frame = frame; 26 | } 27 | 28 | - (CGFloat)y 29 | { 30 | return self.frame.origin.y; 31 | } 32 | 33 | - (void)setCenterX:(CGFloat)centerX 34 | { 35 | CGPoint center = self.center; 36 | center.x = centerX; 37 | self.center = center; 38 | } 39 | 40 | - (CGFloat)centerX 41 | { 42 | return self.center.x; 43 | } 44 | 45 | - (void)setCenterY:(CGFloat)centerY 46 | { 47 | CGPoint center = self.center; 48 | center.y = centerY; 49 | self.center = center; 50 | } 51 | 52 | - (CGFloat)centerY 53 | { 54 | return self.center.y; 55 | } 56 | 57 | - (void)setWidth:(CGFloat)width 58 | { 59 | CGRect frame = self.frame; 60 | frame.size.width = width; 61 | self.frame = frame; 62 | } 63 | 64 | - (CGFloat)width 65 | { 66 | return self.frame.size.width; 67 | } 68 | 69 | - (void)setHeight:(CGFloat)height 70 | { 71 | CGRect frame = self.frame; 72 | frame.size.height = height; 73 | if (height == 0) { 74 | self.hidden = YES; 75 | }else{ 76 | self.hidden = NO; 77 | } 78 | self.frame = frame; 79 | } 80 | 81 | - (CGFloat)height 82 | { 83 | return self.frame.size.height; 84 | } 85 | 86 | - (void)setSize:(CGSize)size 87 | { 88 | // self.width = size.width; 89 | // self.height = size.height; 90 | CGRect frame = self.frame; 91 | frame.size = size; 92 | self.frame = frame; 93 | } 94 | 95 | - (CGSize)size 96 | { 97 | return self.frame.size; 98 | } 99 | 100 | 101 | - (CGFloat)left { 102 | return self.frame.origin.x; 103 | } 104 | 105 | - (void)setLeft:(CGFloat)x { 106 | CGRect frame = self.frame; 107 | frame.origin.x = x; 108 | self.frame = frame; 109 | } 110 | 111 | - (CGFloat)top { 112 | return self.frame.origin.y; 113 | } 114 | 115 | - (void)setTop:(CGFloat)y { 116 | CGRect frame = self.frame; 117 | frame.origin.y = y; 118 | self.frame = frame; 119 | } 120 | 121 | - (CGFloat)right { 122 | return self.frame.origin.x + self.frame.size.width; 123 | } 124 | 125 | - (void)setRight:(CGFloat)right { 126 | CGRect frame = self.frame; 127 | frame.origin.x = right - frame.size.width; 128 | self.frame = frame; 129 | } 130 | 131 | - (CGFloat)bottom { 132 | return self.frame.origin.y + self.frame.size.height; 133 | } 134 | 135 | - (void)setBottom:(CGFloat)bottom { 136 | CGRect frame = self.frame; 137 | frame.origin.y = bottom - frame.size.height; 138 | self.frame = frame; 139 | } 140 | @end 141 | -------------------------------------------------------------------------------- /STTextView/ViewController.h: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.h 3 | // STTextView 4 | // 5 | // Created by StriEver on 17/5/22. 6 | // Copyright © 2017年 StriEver. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | @interface ViewController : UIViewController 12 | 13 | 14 | @end 15 | 16 | -------------------------------------------------------------------------------- /STTextView/ViewController.m: -------------------------------------------------------------------------------- 1 | // 2 | // ViewController.m 3 | // STTextView 4 | // 5 | // Created by StriEver on 17/5/22. 6 | // Copyright © 2017年 StriEver. All rights reserved. 7 | // 8 | 9 | #import "ViewController.h" 10 | #import "STTextView.h" 11 | @interface ViewController () 12 | @property (weak, nonatomic) IBOutlet STTextView *textView; 13 | @property (weak, nonatomic) IBOutlet NSLayoutConstraint *textViewHeight; 14 | 15 | @end 16 | 17 | @implementation ViewController 18 | 19 | - (void)viewDidLoad { 20 | [super viewDidLoad]; 21 | STTextView * tv = [[STTextView alloc]initWithFrame:CGRectMake(10, 100, self.view.frame.size.width - 20, 20)]; 22 | tv.layer.borderColor = [UIColor lightGrayColor].CGColor; 23 | tv.layer.borderWidth = 1; 24 | [self.view addSubview:tv]; 25 | tv.placeholder = @"我是占位符"; 26 | tv.placeholderColor = [UIColor lightGrayColor]; 27 | tv.verticalSpacing = 10; 28 | tv.textContainerInset = UIEdgeInsetsMake(15, 10, 15, 10); 29 | tv.textViewAutoHeight = ^(CGFloat height){ 30 | }; 31 | tv.maxHeight = 200; 32 | tv.minHeight = 35; 33 | tv.backgroundColor = [UIColor whiteColor]; 34 | tv.text = @""; 35 | 36 | 37 | // STTextView * tv1 = [[STTextView alloc]initWithFrame:CGRectMake(10, 210, self.view.frame.size.width - 20, 20)]; 38 | // tv1.backgroundColor = [UIColor greenColor]; 39 | // [self.view addSubview:tv1]; 40 | // tv1.isAutoHeight = NO; 41 | } 42 | 43 | - (void)viewDidLayoutSubviews{ 44 | [super viewDidLayoutSubviews]; 45 | 46 | } 47 | - (void)didReceiveMemoryWarning { 48 | [super didReceiveMemoryWarning]; 49 | // Dispose of any resources that can be recreated. 50 | } 51 | 52 | 53 | @end 54 | -------------------------------------------------------------------------------- /STTextView/main.m: -------------------------------------------------------------------------------- 1 | // 2 | // main.m 3 | // STTextView 4 | // 5 | // Created by StriEver on 17/5/22. 6 | // Copyright © 2017年 StriEver. All rights reserved. 7 | // 8 | 9 | #import 10 | #import "AppDelegate.h" 11 | 12 | int main(int argc, char * argv[]) { 13 | @autoreleasepool { 14 | return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); 15 | } 16 | } 17 | --------------------------------------------------------------------------------